网站的类型有哪些(9种管理类型)

一HttpEntity的类型 1  BasicHttpEntity     代表底层流的基本实体。通常是在http报文中获取的实体。他只有一个空参的构造方法。刚创建时没有内容,长度为负值。需要通过两个方法,把值赋进去。  /** *BasicHttpEntity *@throwsIOException */ public…

大家好,又见面了,我是你们的朋友全栈君。

一 HttpEntity的类型

 

1  BasicHttpEntity

      代表底层流的基本实体。通常是在http报文中获取的实体。他只有一个空参的构造方法。刚创建时没有内容,长度为负值。需要通过两个方法,把值赋进去。

 
  1. /**

  2. * BasicHttpEntity

  3. * @throws IOException

  4. */

  5. public static void testBasicHttpEntity() throws IOException{

  6. InputStream is = null;

  7. //BasicHttpEntity这类就是一个输入流的内容包装类,包装内容的相关的编码格式,长度等

  8. BasicHttpEntity entity = new BasicHttpEntity();

  9. //设置内容

  10. entity.setContent(is);

  11. //设置长度

  12. entity.setContentLength(is.available());

  13. //没搞懂chunked这个属性啥意思

  14. entity.setChunked(false);

  15. }

2  ByteArrayEntity

    是自我包含的,可重复获得使用的,从指定的字节数组中取出内容的实体。字节数组是这个实体的构造方法的参数。

 
  1. /**

  2. * ByteArrayEntity

  3. * @throws IOException

  4. */

  5. public static void testByteArrayEntity() throws IOException{

  6. ByteArrayEntity entity = new ByteArrayEntity("内容".getBytes());

  7. ByteArrayInputStream is = (ByteArrayInputStream) entity.getContent();

  8. //上面这行代码返回的其实是一个ByteArrayInputStream对象

  9. /*public InputStream getContent() {

  10. return new ByteArrayInputStream(this.b, this.off, this.len);

  11. }*/

  12. }

3  StringEntity

    是自我包含的可重复的实体。通过String创建的实体。有两个构造方法,一个是自Sring为参数的构造方法,一个是以String和字符编码为参数的构造方法。

 
  1. /**

  2. * StringEntity

  3. * @throws IOException

  4. */

  5. public static void testStringEntity() throws IOException{

  6. StringBuilder sb = new StringBuilder();

  7. //获取系统的信息集合,这个集合是不可以修改的

  8. Map<String, String> nev = System.getenv();

  9. for(Entry<String, String> entry : nev.entrySet()){

  10. sb.append(entry.getKey()).append("=")

  11. .append(entry.getValue()).append("\n");

  12. }

  13. String content = sb.toString();

  14. System.out.println(content);

  15. //创建只带字符串参数的

  16. StringEntity entity = new StringEntity(content);

  17. //创建带字符创参数和字符编码的

  18. StringEntity entity2 = new StringEntity(content, "UTF-8");

  19.  
  20. }

4  InputreamEntity

    是流式不可以重复的实体。构造方法是InputStream 和内容长度,内容长度是输入流的长度。

 
  1. /**

  2. * InputStreamEntity

  3. * @throws IOException

  4. */

  5. public static void testInputStreamEntity() throws IOException{

  6. InputStream is = null;

  7. //InputStreamEntity严格是对内容和长度相匹配的。用法和BasicHttpEntity类似

  8. InputStreamEntity entity = new InputStreamEntity(is, is.available());

  9. }

5  FileEntity

    自我包含式,可以重复的实体。参数传入文件和文件类型。

 
  1. /**

  2. * FileEntity

  3. * @throws IOException

  4. */

  5. public static void testFileEntity() throws IOException{

  6. FileEntity entity = new FileEntity(new File(""), ContentType.APPLICATION_FORM_URLENCODED);

  7. FileEntity entity2 = new FileEntity(new File(""), "application/java-achive");

  8.  
  9. }

6  EntityTemplete

    从ContentProducer接口接受内容的实体。在ContentProducer的实现类中写入想要写入的内容。

 
  1. /**

  2. * EntityTemplate

  3. * @throws IOException

  4. */

  5. public static void testEntityTemplate() throws IOException{

  6. ContentProducer producer = new ContentProducer() {

  7.  
  8. @Override

  9. public void writeTo(OutputStream outstream) throws IOException {

  10. outstream.write("这是什么东东》。".getBytes());

  11. }

  12. };

  13.  
  14. EntityTemplate entity = new EntityTemplate(producer);

  15. entity.writeTo(System.out);

  16. }

7  HttpEntityWrapper

      这个是创建被包装实体的基类,有被包装实体的引用。相当于实体的代理类,被包装实体是他的一个属性。下面是这个类的源码:

 
  1. /*

  2. * ====================================================================

  3. * Licensed to the Apache Software Foundation (ASF) under one

  4. * or more contributor license agreements. See the NOTICE file

  5. * distributed with this work for additional information

  6. * regarding copyright ownership. The ASF licenses this file

  7. * to you under the Apache License, Version 2.0 (the

  8. * "License"); you may not use this file except in compliance

  9. * with the License. You may obtain a copy of the License at

  10. *

  11. * http://www.apache.org/licenses/LICENSE-2.0

  12. *

  13. * Unless required by applicable law or agreed to in writing,

  14. * software distributed under the License is distributed on an

  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

  16. * KIND, either express or implied. See the License for the

  17. * specific language governing permissions and limitations

  18. * under the License.

  19. * ====================================================================

  20. *

  21. * This software consists of voluntary contributions made by many

  22. * individuals on behalf of the Apache Software Foundation. For more

  23. * information on the Apache Software Foundation, please see

  24. * <http://www.apache.org/>.

  25. *

  26. */

  27.  
  28. package org.apache.http.entity;

  29.  
  30. import java.io.IOException;

  31. import java.io.InputStream;

  32. import java.io.OutputStream;

  33.  
  34. import org.apache.http.Header;

  35. import org.apache.http.HttpEntity;

  36. import org.apache.http.annotation.NotThreadSafe;

  37.  
  38. /**

  39. * Base class for wrapping entities.

  40. * Keeps a {@link #wrappedEntity wrappedEntity} and delegates all

  41. * calls to it. Implementations of wrapping entities can derive

  42. * from this class and need to override only those methods that

  43. * should not be delegated to the wrapped entity.

  44. *

  45. * @since 4.0

  46. */

  47. @NotThreadSafe

  48. public class HttpEntityWrapper implements HttpEntity {

  49.  
  50. /** The wrapped entity. */

  51. protected HttpEntity wrappedEntity;

  52.  
  53. /**

  54. * Creates a new entity wrapper.

  55. *

  56. * @param wrapped the entity to wrap, not null

  57. * @throws IllegalArgumentException if wrapped is null

  58. */

  59. public HttpEntityWrapper(HttpEntity wrapped) {

  60. super();

  61.  
  62. if (wrapped == null) {

  63. throw new IllegalArgumentException

  64. ("wrapped entity must not be null");

  65. }

  66. wrappedEntity = wrapped;

  67.  
  68. } // constructor

  69.  
  70.  
  71. public boolean isRepeatable() {

  72. return wrappedEntity.isRepeatable();

  73. }

  74.  
  75. public boolean isChunked() {

  76. return wrappedEntity.isChunked();

  77. }

  78.  
  79. public long getContentLength() {

  80. return wrappedEntity.getContentLength();

  81. }

  82.  
  83. public Header getContentType() {

  84. return wrappedEntity.getContentType();

  85. }

  86.  
  87. public Header getContentEncoding() {

  88. return wrappedEntity.getContentEncoding();

  89. }

  90.  
  91. public InputStream getContent()

  92. throws IOException {

  93. return wrappedEntity.getContent();

  94. }

  95.  
  96. public void writeTo(OutputStream outstream)

  97. throws IOException {

  98. wrappedEntity.writeTo(outstream);

  99. }

  100.  
  101. public boolean isStreaming() {

  102. return wrappedEntity.isStreaming();

  103. }

  104.  
  105. /**

  106. * @deprecated (4.1) Either use {@link #getContent()} and call {@link java.io.InputStream#close()} on that;

  107. * otherwise call {@link #writeTo(OutputStream)} which is required to free the resources.

  108. */

  109. @Deprecated

  110. public void consumeContent() throws IOException {

  111. wrappedEntity.consumeContent();

  112. }

  113.  
  114. }

  115.  

8  BufferedHttpEntity

    是HttpEntityWarpper的子类,可以把不可以重复的实体,实现成可以重复的实体。它从提供的实体中读取内容,缓存到内容中。源码如下:

 
  1. /*

  2. * ====================================================================

  3. * Licensed to the Apache Software Foundation (ASF) under one

  4. * or more contributor license agreements. See the NOTICE file

  5. * distributed with this work for additional information

  6. * regarding copyright ownership. The ASF licenses this file

  7. * to you under the Apache License, Version 2.0 (the

  8. * "License"); you may not use this file except in compliance

  9. * with the License. You may obtain a copy of the License at

  10. *

  11. * http://www.apache.org/licenses/LICENSE-2.0

  12. *

  13. * Unless required by applicable law or agreed to in writing,

  14. * software distributed under the License is distributed on an

  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

  16. * KIND, either express or implied. See the License for the

  17. * specific language governing permissions and limitations

  18. * under the License.

  19. * ====================================================================

  20. *

  21. * This software consists of voluntary contributions made by many

  22. * individuals on behalf of the Apache Software Foundation. For more

  23. * information on the Apache Software Foundation, please see

  24. * <http://www.apache.org/>.

  25. *

  26. */

  27.  
  28. package org.apache.http.entity;

  29.  
  30. import java.io.ByteArrayInputStream;

  31. import java.io.IOException;

  32. import java.io.InputStream;

  33. import java.io.OutputStream;

  34.  
  35. import org.apache.http.HttpEntity;

  36. import org.apache.http.annotation.NotThreadSafe;

  37. import org.apache.http.util.EntityUtils;

  38.  
  39. /**

  40. * A wrapping entity that buffers it content if necessary.

  41. * The buffered entity is always repeatable.

  42. * If the wrapped entity is repeatable itself, calls are passed through.

  43. * If the wrapped entity is not repeatable, the content is read into a

  44. * buffer once and provided from there as often as required.

  45. *

  46. * @since 4.0

  47. */

  48. @NotThreadSafe

  49. public class BufferedHttpEntity extends HttpEntityWrapper {

  50.  
  51. private final byte[] buffer;

  52.  
  53. /**

  54. * Creates a new buffered entity wrapper.

  55. *

  56. * @param entity the entity to wrap, not null

  57. * @throws IllegalArgumentException if wrapped is null

  58. */

  59. public BufferedHttpEntity(final HttpEntity entity) throws IOException {

  60. super(entity);

  61. if (!entity.isRepeatable() || entity.getContentLength() < 0) {

  62. this.buffer = EntityUtils.toByteArray(entity);

  63. } else {

  64. this.buffer = null;

  65. }

  66. }

  67.  
  68. @Override

  69. public long getContentLength() {

  70. if (this.buffer != null) {

  71. return this.buffer.length;

  72. } else {

  73. return wrappedEntity.getContentLength();

  74. }

  75. }

  76.  
  77. @Override

  78. public InputStream getContent() throws IOException {

  79. if (this.buffer != null) {

  80. return new ByteArrayInputStream(this.buffer);

  81. } else {

  82. return wrappedEntity.getContent();

  83. }

  84. }

  85.  
  86. /**

  87. * Tells that this entity does not have to be chunked.

  88. *

  89. * @return <code>false</code>

  90. */

  91. @Override

  92. public boolean isChunked() {

  93. return (buffer == null) && wrappedEntity.isChunked();

  94. }

  95.  
  96. /**

  97. * Tells that this entity is repeatable.

  98. *

  99. * @return <code>true</code>

  100. */

  101. @Override

  102. public boolean isRepeatable() {

  103. return true;

  104. }

  105.  
  106.  
  107. @Override

  108. public void writeTo(final OutputStream outstream) throws IOException {

  109. if (outstream == null) {

  110. throw new IllegalArgumentException("Output stream may not be null");

  111. }

  112. if (this.buffer != null) {

  113. outstream.write(this.buffer);

  114. } else {

  115. wrappedEntity.writeTo(outstream);

  116. }

  117. }

  118.  
  119.  
  120. // non-javadoc, see interface HttpEntity

  121. @Override

  122. public boolean isStreaming() {

  123. return (buffer == null) && wrappedEntity.isStreaming();

  124. }

  125.  
  126. } // class BufferedHttpEntity

  127.  

二 HttpEntity 的使用

 

1  HttpEntity实体即可以使流也可以使字符串形式。具体有什么用法看他的方法解释:

 
  1. package com.scl.base;

  2.  
  3. import java.io.IOException;

  4. import java.io.UnsupportedEncodingException;

  5.  
  6. import org.apache.http.HttpEntity;

  7. import org.apache.http.ParseException;

  8. import org.apache.http.entity.StringEntity;

  9. import org.apache.http.util.EntityUtils;

  10.  
  11. public class HttpClientDemo06 {

  12.  
  13. /**

  14. * @param args

  15. */

  16. public static void main(String[] args) {

  17. try {

  18. HttpEntity entity = new StringEntity("这一个字符串实体", "UTF-8");

  19. //内容类型

  20. System.out.println(entity.getContentType());

  21. //内容的编码格式

  22. System.out.println(entity.getContentEncoding());

  23. //内容的长度

  24. System.out.println(entity.getContentLength());

  25. //把内容转成字符串

  26. System.out.println(EntityUtils.toString(entity));

  27. //内容转成字节数组

  28. System.out.println(EntityUtils.toByteArray(entity).length);

  29. //还有个直接获得流

  30. //entity.getContent();

  31. } catch (UnsupportedEncodingException e) {

  32. throw new RuntimeException(e);

  33. } catch (ParseException e) {

  34. } catch (IOException e) {

  35. }

  36.  
  37. }

  38.  
  39. }

2  对于实体的资源使用完之后要适当的回收资源,特别是对于流实体。例子代码如下:

 
  1. public static void test() throws IllegalStateException, IOException{

  2. HttpResponse response = null;

  3. HttpEntity entity = response.getEntity();

  4.  
  5. if(entity!=null){

  6.  
  7. InputStream is = entity.getContent();

  8. try{

  9. //做一些操作

  10. }finally{

  11. //最后别忘了关闭应该关闭的资源,适当的释放资源

  12. if(is != null){

  13. is.close();

  14. }

  15. //这个方法也可以把底层的流给关闭了

  16. EntityUtils.consume(entity);

  17. //下面是这方法的源码

  18. /*public static void consume(final HttpEntity entity) throws IOException {

  19. if (entity == null) {

  20. return;

  21. }

  22. if (entity.isStreaming()) {

  23. InputStream instream = entity.getContent();

  24. if (instream != null) {

  25. instream.close();

  26. }

  27. }

  28. }*/

  29. }

  30. }

FROM: http://blog.csdn.net/athenamax/article/details/8185043http://blog.csdn.net/athenamax/article/details/8185041

原文字体太小,看着眼睛疼,特此加大字体转载

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/125632.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 我自己实际操作安装MyCat水平分割之分片枚举和取模算法(二)

    我自己实际操作安装MyCat水平分割之分片枚举和取模算法(二)

  • 查看mysql日志命令_linux查看mysql安装路径

    查看mysql日志命令_linux查看mysql安装路径centos是linux吗_网站服务器运行维护centos是一个基于RedHatLinux提供的可自由使用源代码的企业级Linux发行版本,它是来自于RedHatEnterpriseLinux依照开放源代码规定释出的源代码所编译而成。Linux中MySQL日志在哪Linux中MySQL日志一般保存在/var/log/目录下,但还需要看具体的配置文件才能确定,具体方法如下:1、首先登陆…

    2022年10月14日
  • ResNet+FPN实现+白嫖代码「建议收藏」

    ResNet+FPN实现+白嫖代码「建议收藏」===========================================================有现成的代码:https://github.com/Kongsea/FPN_TensorFlow推荐根据该博客来学习:https://www.jianshu.com/p/324af87a11a6============================================================纸上得来终觉浅,须知此事要coding!ResNet+FPN

  • python图层合并_Photoshop_【批量将同一背景与不同的上层合并图层的技巧】导出+Python3.X实现…[通俗易懂]

    python图层合并_Photoshop_【批量将同一背景与不同的上层合并图层的技巧】导出+Python3.X实现…[通俗易懂]设计需求:现在要制作一系列展品的小标签,使用一份相同的背景,改动的仅是文字内容设计环境:AdobePhotoshopCC2017Python3.X技巧思路:用Photoshop自带的功能将每个图层输出为PNG格式到某一文件夹使用python的PIL的Image模块,批量合成根据此次的文字居中要求,合成时的坐标经过计算(应该是前景的左上角在背景上的坐标,此坐标系的原点为左上角)Python…

    2022年10月23日
  • 汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF

    汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF**CF:**进位标志位。在无符号运算时,记录了运算结果的最高有效位向更高位的进位值或从更高位借位,产生进位或借位时CF=1,否则CF=0;**PF:**奇偶标志位。相关指令执行后结果所有bit中1的个数为偶数,那么PF=1,1的个数为奇数则PF=0;**AF:**辅助进位标志位。运算过程中看最后四位,不论长度为多少。最后四位向前有进位或者借位,AF=1,否则AF=0;**ZF:**零标志位…

  • idea 最新激活码(注册激活)「建议收藏」

    (idea 最新激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~https://javaforall.cn/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~KU…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号