EntityUtils 工具类

EntityUtils对象是org.apache.http.util下的一个工具类,用官方的解释是为HttpEntity对象提供的静态帮助类

package org.apache.http.util;import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.entity.ContentType;
import org.apache.http.protocol.HTTP;/*** Static helpers for dealing with {@link HttpEntity}s.** @since 4.0*/
public final class EntityUtils {private EntityUtils() {}/*** Ensures that the entity content is fully consumed and the content stream, if exists,* is closed. The process is done, <i>quietly</i> , without throwing any IOException.** @param entity the entity to consume.*** @since 4.2*/public static void consumeQuietly(final HttpEntity entity) {try {consume(entity);} catch (final IOException ignore) {}}/*** Ensures that the entity content is fully consumed and the content stream, if exists,* is closed.** @param entity the entity to consume.* @throws IOException if an error occurs reading the input stream** @since 4.1*/public static void consume(final HttpEntity entity) throws IOException {if (entity == null) {return;}if (entity.isStreaming()) {final InputStream instream = entity.getContent();if (instream != null) {instream.close();}}}/*** Updates an entity in a response by first consuming an existing entity, then setting the new one.** @param response the response with an entity to update; must not be null.* @param entity the entity to set in the response.* @throws IOException if an error occurs while reading the input stream on the existing* entity.* @throws IllegalArgumentException if response is null.** @since 4.3*/public static void updateEntity(final HttpResponse response, final HttpEntity entity) throws IOException {Args.notNull(response, "Response");consume(response.getEntity());response.setEntity(entity);}/*** Read the contents of an entity and return it as a byte array.** @param entity the entity to read from=* @return byte array containing the entity content. May be null if*   {@link HttpEntity#getContent()} is null.* @throws IOException if an error occurs reading the input stream* @throws IllegalArgumentException if entity is null or if content length &gt; Integer.MAX_VALUE*/public static byte[] toByteArray(final HttpEntity entity) throws IOException {Args.notNull(entity, "Entity");final InputStream instream = entity.getContent();if (instream == null) {return null;}try {Args.check(entity.getContentLength() <= Integer.MAX_VALUE,"HTTP entity too large to be buffered in memory");int i = (int)entity.getContentLength();if (i < 0) {i = 4096;}final ByteArrayBuffer buffer = new ByteArrayBuffer(i);final byte[] tmp = new byte[4096];int l;while((l = instream.read(tmp)) != -1) {buffer.append(tmp, 0, l);}return buffer.toByteArray();} finally {instream.close();}}/*** Obtains character set of the entity, if known.** @param entity must not be null* @return the character set, or null if not found* @throws ParseException if header elements cannot be parsed* @throws IllegalArgumentException if entity is null** @deprecated (4.1.3) use {@link ContentType#getOrDefault(HttpEntity)}*/@Deprecatedpublic static String getContentCharSet(final HttpEntity entity) throws ParseException {Args.notNull(entity, "Entity");String charset = null;if (entity.getContentType() != null) {final HeaderElement values[] = entity.getContentType().getElements();if (values.length > 0) {final NameValuePair param = values[0].getParameterByName("charset");if (param != null) {charset = param.getValue();}}}return charset;}/*** Obtains MIME type of the entity, if known.** @param entity must not be null* @return the character set, or null if not found* @throws ParseException if header elements cannot be parsed* @throws IllegalArgumentException if entity is null** @since 4.1** @deprecated (4.1.3) use {@link ContentType#getOrDefault(HttpEntity)}*/@Deprecatedpublic static String getContentMimeType(final HttpEntity entity) throws ParseException {Args.notNull(entity, "Entity");String mimeType = null;if (entity.getContentType() != null) {final HeaderElement values[] = entity.getContentType().getElements();if (values.length > 0) {mimeType = values[0].getName();}}return mimeType;}private static String toString(final HttpEntity entity,final ContentType contentType) throws IOException {final InputStream instream = entity.getContent();if (instream == null) {return null;}try {Args.check(entity.getContentLength() <= Integer.MAX_VALUE,"HTTP entity too large to be buffered in memory");int i = (int)entity.getContentLength();if (i < 0) {i = 4096;}Charset charset = null;if (contentType != null) {charset = contentType.getCharset();if (charset == null) {final ContentType defaultContentType = ContentType.getByMimeType(contentType.getMimeType());charset = defaultContentType != null ? defaultContentType.getCharset() : null;}}if (charset == null) {charset = HTTP.DEF_CONTENT_CHARSET;}final Reader reader = new InputStreamReader(instream, charset);final CharArrayBuffer buffer = new CharArrayBuffer(i);final char[] tmp = new char[1024];int l;while((l = reader.read(tmp)) != -1) {buffer.append(tmp, 0, l);}return buffer.toString();} finally {instream.close();}}/*** Get the entity content as a String, using the provided default character set* if none is found in the entity.* If defaultCharset is null, the default "ISO-8859-1" is used.** @param entity must not be null* @param defaultCharset character set to be applied if none found in the entity,* or if the entity provided charset is invalid or not available.* @return the entity content as a String. May be null if*   {@link HttpEntity#getContent()} is null.* @throws ParseException if header elements cannot be parsed* @throws IllegalArgumentException if entity is null or if content length &gt; Integer.MAX_VALUE* @throws IOException if an error occurs reading the input stream* @throws java.nio.charset.UnsupportedCharsetException Thrown when the named entity's charset is not available in* this instance of the Java virtual machine and no defaultCharset is provided.*/public static String toString(final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {Args.notNull(entity, "Entity");ContentType contentType = null;try {contentType = ContentType.get(entity);} catch (final UnsupportedCharsetException ex) {if (defaultCharset == null) {throw new UnsupportedEncodingException(ex.getMessage());}}if (contentType != null) {if (contentType.getCharset() == null) {contentType = contentType.withCharset(defaultCharset);}} else {contentType = ContentType.DEFAULT_TEXT.withCharset(defaultCharset);}return toString(entity, contentType);}/*** Get the entity content as a String, using the provided default character set* if none is found in the entity.* If defaultCharset is null, the default "ISO-8859-1" is used.** @param entity must not be null* @param defaultCharset character set to be applied if none found in the entity* @return the entity content as a String. May be null if*   {@link HttpEntity#getContent()} is null.* @throws ParseException if header elements cannot be parsed* @throws IllegalArgumentException if entity is null or if content length &gt; Integer.MAX_VALUE* @throws IOException if an error occurs reading the input stream* @throws java.nio.charset.UnsupportedCharsetException Thrown when the named charset is not available in* this instance of the Java virtual machine*/public static String toString(final HttpEntity entity, final String defaultCharset) throws IOException, ParseException {return toString(entity, defaultCharset != null ? Charset.forName(defaultCharset) : null);}/*** Read the contents of an entity and return it as a String.* The content is converted using the character set from the entity (if any),* failing that, "ISO-8859-1" is used.** @param entity the entity to convert to a string; must not be null* @return String containing the content.* @throws ParseException if header elements cannot be parsed* @throws IllegalArgumentException if entity is null or if content length &gt; Integer.MAX_VALUE* @throws IOException if an error occurs reading the input stream* @throws java.nio.charset.UnsupportedCharsetException Thrown when the named charset is not available in* this instance of the Java virtual machine*/public static String toString(final HttpEntity entity) throws IOException, ParseException {Args.notNull(entity, "Entity");return toString(entity, ContentType.get(entity));}}

EntityUtils 工具类相关推荐

  1. httpclient工具类,post请求发送json字符串参数,中文乱码处理

    在使用httpclient发送post请求的时候,接收端中文乱码问题解决. 正文: 我们都知道,一般情况下使用post请求是不会出现中文乱码的.可是在使用httpclient发送post请求报文含中文 ...

  2. java录排名怎么写_面试官:Java排名靠前的工具类你都用过哪些?

    你知道的越多,不知道的就越多,业余的像一棵小草! 你来,我们一起精进!你不来,我和你的竞争对手一起精进! 编辑:业余草 推荐:https://www.xttblog.com/?p=5158 在Java ...

  3. 干货:排名前 16 的 Java 工具类!

    2019独角兽企业重金招聘Python工程师标准>>> 干货:排名前 16 的 Java 工具类!   在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最 ...

  4. 排名前 16 的 Java 工具类

    转载来自微信公众号:Java 技术栈.如有侵权,请联系作者删除!! 在 Java 中,工具类定义了一组公共方法,这篇文章将介绍 Java 中使用最频繁及最通用的 Java 工具类.以下工具类.方法按使 ...

  5. 排名前 16 的 Java 工具类,哪个你没用过?

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 作者 | JAVA葵花宝典-整理翻译 来源 | https ...

  6. beanutils工具类_16 个超级实用的 Java 工具类!

    在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...

  7. simplexmlelement类设置编码_「软帝学院」:2019java五大常用工具类整理

    1.json转换工具 1. package com.taotao.utils; 3. import java.util.List; 5. import com.fasterxml.jackson.co ...

  8. Android开发之使用Handler封装下载图片工具类(源代码分享)

    如果每下载一张图片,就得重写一次Http协议,多线程的启动和handler的信息传递就显得太麻烦了,我们直接来封装一个工具类,便于我们以后在开发时随时可以调用. (1)在清单文件添加权限 <us ...

  9. java轻量级并行工具类_16 个超级实用的 Java 工具类

    原标题:16 个超级实用的 Java 工具类 源 /juejin 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名, ...

最新文章

  1. ubuntu mysql 远程连接问题解决方法
  2. MVC中,视图的Layout使用
  3. 牛客多校2 - All with Pairs(字符串哈希+next数组)
  4. 3pc_three phase commit protocol协议理解
  5. “天才”少年!4位90后摘得全球顶尖数学大奖,90%获奖者不满30岁
  6. 从数学基础到贝叶斯理论到实践——深度AI科普团队
  7. python3基本数据类型02——列表、元组
  8. [PHP] - Laravel 5 的 Hello Wold
  9. 有哪些免费的绘画软件比较好用?
  10. android屏幕内容实时传输,在设备之间无缝传输内容
  11. 最速下降法matlab全局最小值_MATLAB实现最速下降法
  12. Exchange2010 server的部署及配置(一)
  13. 内存继续涨价 LPDDR4/LPDDR4X内存标准升级
  14. winpe加载raid_WinPE安装RAID卡驱动的详细教程
  15. html透明度从零到1,CSS过渡不透明度仅从0到1,或其他过渡效果
  16. 5、win7激活秘钥
  17. linux sfc模拟器,PSP适用SFC模拟器Snes9x完全使用教程
  18. Web前端系列技术之移动端CSDN会员页面复刻(动态完整版)
  19. 邮箱验证(正则表达式)
  20. 语音识别开发---基于科大讯飞开放平台

热门文章

  1. 95后程序员连续15天加班到凌晨2点在餐厅泪崩!看到955不加班的公司名单,酸哭了......
  2. 如何Excel中找出不同两列的相同值
  3. 网络工程师的就业前景、职业规划和工资待遇
  4. 2019(石家庄)国际消防设备技术及安全产业博览会
  5. Android旗舰机与苹果,安卓与苹果两大旗舰,iphone 11+一加8 Pro,选哪个?
  6. [Swift]圆角处理
  7. mini2440驱动奇谭——helloworld
  8. 苹果电脑(Macbook Pro)开机后没有声音的解决
  9. 今天第五人格服务器维护,更新公告《第五人格》2021年5月8日维护公告
  10. Node.js学习五(事件)