- 浏览: 834238 次
- 性别:
- 来自: 厦门
文章分类
- 全部博客 (363)
- 2010年3月 (3)
- 2010年4月 (3)
- Java (116)
- ExtJs (22)
- EJB3.0 (11)
- JQuery (28)
- SqlServer (5)
- Oracle (17)
- hibernate (21)
- struts2 (14)
- php (10)
- JavaScript (11)
- jbpm (6)
- spring (24)
- lucene (2)
- ibatis (7)
- C# (8)
- mysql (11)
- json (3)
- webservice (3)
- 设计模式 (1)
- jdbc (1)
- servlet (2)
- ice (6)
- 日常软件问题 (2)
- 生活 (2)
- iphone (1)
- rest (3)
- ruby (2)
- linux (13)
- quartz (1)
- poi (1)
- redis (13)
- memcached (4)
- nosql (2)
- tomcat调优 (1)
- 项目管理 (0)
最新评论
-
天使建站:
jquery里和数组相关的操作 附带具体的实例 ...
jquery对象数组 -
Cy0941:
$('#formId').form('submit',...) ...
easyui的form表单提交处理 -
shmily2038:
swifth 写道楼主,,你的命令写错啦,,[root@ser ...
centos直接yum安装nginx -
swifth:
楼主,,你的命令写错啦,,[root@server ~]# y ...
centos直接yum安装nginx -
随遇而安DXX:
...
REST
使用HttpClient,总是报出“Going to buffer response body of large or unknown
size. Using getResponseBodyAsStream instead is
recommended.”的WARN日志,定位到HttpClient的源码如下:
public
byte
[
]
getResponseBody(
)
throws
IOException
{
if ( this .responseBody == null ) {
InputStream instream = getResponseBodyAsStream( ) ;
if ( instream != null ) {
long contentLength = getResponseContentLength( ) ;
if ( contentLength > Integer .MAX_VALUE ) { //guard below cast from overflow
throw new IOException ( "Content too large to be buffered: " + contentLength + " bytes" ) ;
}
int limit = getParams( ) .getIntParameter ( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT , 1024 * 1024 ) ;
if ( ( contentLength == - 1 ) || ( contentLength > limit) ) {
LOG.warn ( "Going to buffer response body of large or unknown size. "
+ "Using getResponseBodyAsStream instead is recommended." ) ;
}
LOG.debug ( "Buffering response body" ) ;
ByteArrayOutputStream outstream = new ByteArrayOutputStream (
contentLength > 0 ? ( int ) contentLength : DEFAULT_INITIAL_BUFFER_SIZE) ;
byte [ ] buffer = new byte [ 4096 ] ;
int len;
while ( ( len = instream.read ( buffer) ) > 0 ) {
outstream.write ( buffer, 0 , len) ;
}
outstream.close ( ) ;
setResponseStream( null ) ;
this .responseBody = outstream.toByteArray ( ) ;
}
}
return this .responseBody ;
}
if ( this .responseBody == null ) {
InputStream instream = getResponseBodyAsStream( ) ;
if ( instream != null ) {
long contentLength = getResponseContentLength( ) ;
if ( contentLength > Integer .MAX_VALUE ) { //guard below cast from overflow
throw new IOException ( "Content too large to be buffered: " + contentLength + " bytes" ) ;
}
int limit = getParams( ) .getIntParameter ( HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT , 1024 * 1024 ) ;
if ( ( contentLength == - 1 ) || ( contentLength > limit) ) {
LOG.warn ( "Going to buffer response body of large or unknown size. "
+ "Using getResponseBodyAsStream instead is recommended." ) ;
}
LOG.debug ( "Buffering response body" ) ;
ByteArrayOutputStream outstream = new ByteArrayOutputStream (
contentLength > 0 ? ( int ) contentLength : DEFAULT_INITIAL_BUFFER_SIZE) ;
byte [ ] buffer = new byte [ 4096 ] ;
int len;
while ( ( len = instream.read ( buffer) ) > 0 ) {
outstream.write ( buffer, 0 , len) ;
}
outstream.close ( ) ;
setResponseStream( null ) ;
this .responseBody = outstream.toByteArray ( ) ;
}
}
return this .responseBody ;
}
报WARN的条件是 ((contentLength == -1) || (contentLength >
limit)),也就是说,或者是返回的HTTP头没有指定contentLength,或者是contentLength大于上限(默认是1M)。如果
能确定返回结果的大小对程序没有显著影响,这个WARN就可以忽略,可以在日志的配置中把HttpClient的日志级别调到ERROR,不让它报出来。
当然,这个警告也是有意义的,HttpClient建议使用InputStream
getResponseBodyAsStream()代替byte[]
getResponseBody()。对于返回结果很大或无法预知的情况,就需要使用InputStream
getResponseBodyAsStream(),避免byte[] getResponseBody()可能带来的内存的耗尽问题。
使用String response = method.getResponseBodyAsString().trim();
获得getMethod返回的内容,
每次都出现如下警告:
[WARN org.apache.commons.httpclient.HttpMethodBase] Going to buffer response body of
large or unknown size. Using getResponseBodyAsStream instead is recommended.
解决方法:
- //String response = method.getResponseBodyAsString().trim();
- InputStream resStream = method.getResponseBodyAsStream();
- BufferedReader br = new BufferedReader( new InputStreamReader(resStream));
- StringBuffer resBuffer = new StringBuffer();
- String resTemp = "" ;
- while ((resTemp = br.readLine()) != null ){
- resBuffer.append(resTemp);
- }
- String response = resBuffer.toString();
//String response = method.getResponseBodyAsString().trim();
InputStream resStream = method.getResponseBodyAsStream(); BufferedReader
br = new BufferedReader(new InputStreamReader(resStream)); StringBuffer
resBuffer = new StringBuffer(); String resTemp = ""; while((resTemp =
br.readLine()) != null){ resBuffer.append(resTemp); } String response =
resBuffer.toString();
response
.
addHeader
(
"Content-Type"
,
"text/html; charset=utf-8"
);
response
.
addHeader
(
"Content-Length"
,
String
.
valueOf
(
output
.
getBytes
(
"utf8"
).
length
));
发表评论
-
Jackson2.x通用工具类
2014-11-03 11:38 4909import java.io.IOException; i ... -
面试题
2013-02-27 09:04 1709从1加到100(考虑减少循环次数)使用数学公式 首先要知道 ... -
单例延迟实例化
2013-01-23 08:55 13291.如果出于性能的考虑而需要对实例域使用延迟初始化,就使用双 ... -
java中重载与重写的区别
2013-01-21 10:03 925首先我们来讲讲:重载(Overloading) ( ... -
自定义标签
2012-12-15 12:58 1001package com.fsti.tag; import ... -
JAXB格式化beanToXml
2012-12-14 15:03 1283context = JAXBContext.n ... -
面向对象的特征有哪些方面?
2012-12-13 09:36 1009计算机软件系统是现实 ... -
java nio缓冲器
2012-12-05 17:03 3606缓冲器仅仅是一个" 多功能 " 的数组。可 ... -
面向对象三大特性一句话概括
2012-12-04 15:58 1553封装可以隐藏实现细节,使得代码模块化; 继承可以扩 ... -
Java序列化高级认识
2012-12-04 09:13 1067将 Java 对象序列化为二进制文件的 Java 序列化技术是 ... -
面向接口编程——提升系统多态性和可扩展性
2012-12-03 14:10 1286接口的本质 接口,在表面上是由几个没有主体代码的方 ... -
面向对象之多态
2012-11-23 19:22 875多态性(polymorphisn)是允许你将父对象设置成为和一 ... -
我对"秒杀"在技术性上的一些看法
2012-11-22 11:31 1014秒杀,是指电子商务 ... -
项目编码
2012-11-16 13:59 1158看两个项目所用的编码是否一样 Java的乱码问题: ... -
SVN错误:Attempted to lock an already-locked dir
2012-11-07 09:08 1030出现这个问题后使用“ ... -
面向对象的三个基本特征
2012-11-04 08:31 1088面向对象的三个基本特征是:封装、继承、多态。 封装 封装最 ... -
预编译防sql注入
2012-11-03 20:23 1973prepareStatement会先初始化SQL,先把这个SQ ... -
Java类与对象的初始化
2012-10-19 09:33 897Java类与对象的初始化 面试的时候,经常会遇到这样的笔试题 ... -
commons bean
2012-10-19 09:15 892这是两个javabean对象 package com.bea ... -
java工具DateUtil
2012-10-12 14:42 1054//一年内的周一 public final class Da ...
相关推荐
This book is written with two objective in mind, first, to introduce the reader to the concepts of programming using C#, second, to put into practice the concepts in a fun and entertaining way by ...
Whether you are an ArcGIS user with no background in programming or a programmer without experience with the ArcGIS platform, this book arms you with everything you need to get going with ArcGIS for ...
Adding Instance Functions and Properties to Controllers . . . . . . . . 35 Dependency Injection in Controllers With Minification . . . . . . . . . 37 Overview of Two-Way Data Binding . . . . . . . . ....
本练习题的主题围绕“将要做什么”(What are you going to do),主要考察学生对于将来时态的运用以及询问他人计划的能力。在英语中,将来时态通常用"be going to"结构表示即将发生的动作或计划。下面我们将详细...
This book is for experienced Java programmers or developers looking to further refine or add to their skills and knowledge base. Table of Contents Chapter 1. Going Inside Java 8 Chapter 2. Designing ...
This book is for experienced Java programmers or developers looking to further refine or add to their skills and knowledge base. Table of Contents Chapter 1. Going Inside Java 8 Chapter 2. Designing ...
CommView is a helpful tool for LAN administrators, security professionals, network programmers, or anyone who wants to have a full picture of the traffic going through one's PC or LAN segment....
A 32-bit process is normally limited to addressing 2 gigabytes (GB) of memory, or 3 GB if the system was booted using the /3G boot switch even if there is more physical memory available. By leveraging...
This Is Going to Hurt《疼痛难免(2022)》第一季第四集完整中英文对照剧本 .pdf
"be going to"结构是英语中用来表达将来时态的一种方式,主要分为以下几个方面来讲解: 首先,"be going to"结构的基本形式是"主语 + be going to + 动词原形",这里的"be"动词会随着主语的人称和数发生变化,有am,...
All you need is a solid plan of what your environment is going to look and play like prior to creating it. "Preproduction Blueprint" is the planning system and workbook. These are the same steps I ...
To get the most out of it, you should have a firm grasp of modern JavaScript and some knowledge of how to work with relational databases and the command line. I explain new and interesting bits of ...
know how to define the problems they want to solve using MATLAB and how to use the corresponding routines to solve their problems. We never deny that detailed knowledge about the algorithm (engine) of...
I am not going to present one specific architecture, though. The reason for this is simple enough: websites are much too heterogeneous to be designed in a uniform way. Requirements differ a lot, as do...
In this example we are going to find out the factorial of 12 by using the while loop. In while loop the loop will run until the condition we have given gets true. Calculating factorial After going ...
George Polya revealed how the mathematical method of demonstrating a proof or finding an unknown can be of help in attacking any problem that can be "reasoned" out - from building a bridge to winning ...
There is going to be a concert this weekend.(这个周末将有一场音乐会。) There are going to be many people at the park.(公园里将会有很多人。) 练习: 1. 这里明天将有一场足球比赛。 There _______ __...
The benefit of a column maker is that it can help you to format your text/code, or in some cases to make it easier to read in complex nested logic. Quick Open UltraEdit and UEStudio provide multiple ...