httpclient 从服务端读取大文件

木木水夕 2011-08-15 01:29:00
使用httpclient向服务端发送xml文件,然后服务端经过解析后在发送修改后的xml文件给客户端,现有一问题:当xml文件小的时候一百多K,没有问题,但是当xml为1M时客户端就抛出异常,服务端可以接收到完整的xml内容
异常如下:
Exception in thread "main" org.apache.http.MalformedChunkCodingException: Unexpected content at the end of chunk
at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:239)
at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:204)
at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:167)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:138)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.Reader.read(Unknown Source)
at TestH.main(TestH.java:125)
关键代码:


httppost = new HttpPost(URL);
//请求超时
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, nTimeout);
//读取超时
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, nTimeout);
//写入报文信息
reqEntity.addPart("body", body);
httppost.setEntity(reqEntity);
try {
response = httpclient.execute(httppost);
} catch (IOException e) {
System.out.println("read data timeout");
continue;
}

resEntity = response.getEntity();
inputStream = resEntity.getContent();
InputStreamReader reader = new InputStreamReader(inputStream ,
"UTF-8");
char[] buff = new char[1024];
int length = 0;
while ((length = reader.read(buff)) != -1) {
String x = new String(buff, 0, length);
System.out.println(x);
}

...全文
1226 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
木木水夕 2011-08-17
  • 打赏
  • 举报
回复
搞定。。。。。是我服务端返回时写的不。。。
服务端我是直接使用:
PrintWriter out = resp.getWriter();
out.write(s);
用下面这个就不会出错:
OutputStream out = resp.getOutputStream();
OutputStreamWriter ouputw = new OutputStreamWriter(out,"UTF-8");
String str2 = sb.toString();
ouputw.write(str2);
luohuijun619 2011-08-15
  • 打赏
  • 举报
回复
呵呵,不好意思,直接拷贝代码的,忘了改了
softroad 2011-08-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 luohuijun619 的回复:]

StringEntity entitys = new StringEntity(jsonObject.toString());
entitys.setContentType("application/json");
entitys.setChunked(true);
[/Quote]

我lz返回的是xml,你怎么设成json了?
entitys.setContentType("text/xml");
entitys.setChunked(true);
木木水夕 2011-08-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 luohuijun619 的回复:]
StringEntity entitys = new StringEntity(jsonObject.toString());
entitys.setContentType("application/json");
entitys.setChunked(true);
[/Quote]
能否解释的清楚点?
我嘞个去 2011-08-15
  • 打赏
  • 举报
回复
没遇见过这种问题,友情帮顶。。。。
luohuijun619 2011-08-15
  • 打赏
  • 举报
回复
StringEntity entitys = new StringEntity(jsonObject.toString());
entitys.setContentType("application/json");
entitys.setChunked(true);
安心逍遥 2011-08-15
  • 打赏
  • 举报
回复
int readCount = innerReader.ReadContentAsBase64(buffer, index, count);

if (readCount == 0)

{

//GetReaderFromNextChunk will wait for chunks to be queued

//then set this.innerReader to the received message's reader

//it will throw TimeoutException if the next chunk is not received within specified timeout

GetReaderFromNextChunk(receiveTimeout);

if (!isLastChunk)

{

readCount = innerReader.ReadContentAsBase64(buffer,

index, count);

if (readCount == 0)

{

throw new CommunicationException("Received chunk contains no data");

}

else

{

return readCount;

}

}

else //lastChunk

{

return 0;

}

}

else

{

return readCount;

}

67,545

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧