您的位置:首页 > 理论基础 > 计算机网络

HTTPCLIENT POST 返回信息处理 避免返回数据过大

2007-06-06 10:51 591 查看
public final int PAGE_MAX_LEN = 1024 * 1024;

//调用

ss = getHTMLcontent(post.getResponseBodyAsStream(), post.getResponseContentLength(), post.getResponseCharSet());

//函数
public String getHTMLcontent(InputStream is,long contentLength,String charSet) throws IOException
{
byte[] responseBody = null;
if (is != null) {
if ((contentLength != -1) && (contentLength > PAGE_MAX_LEN)) {
throw new HttpContentTooLargeException("Content-Length is "
+ contentLength, PAGE_MAX_LEN);
}
ByteArrayOutputStream rawdata = new ByteArrayOutputStream(
contentLength > 0 ? (int) contentLength : 4 * 1024);
byte[] buffer = new byte[2048];
int pos = 0;
int len;
do {
len = is.read(buffer, 0, Math.min(buffer.length,
PAGE_MAX_LEN - pos));
if (len == -1)
break;
rawdata.write(buffer, 0, len);
pos += len;
} while (pos < PAGE_MAX_LEN);

// check if there is even more data
if (pos == PAGE_MAX_LEN) {
if (is.read() != -1)
throw new HttpContentTooLargeException(
"Content-Length not known but larger than "
+ PAGE_MAX_LEN, PAGE_MAX_LEN);
}
responseBody = rawdata.toByteArray();
}
return EncodingUtil.getString(responseBody, charSet);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐