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

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);
 } 

你可能感兴趣的:(HTTPCLIENT POST 返回信息处理 避免返回数据过大)