问题似乎只发生在我试图发布的“大”文件中。
我的代码如下所示:
PostMethod method = new PostMethod(url);
File input = new File(filePathname);
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
method.setRequestEntity(entity);
method.setRequestHeader("Content-Disposition", "attachment; filename=xyzzy")
HttpClient client = new HttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials("userid", "pw");
client.getState().setCredentials(new AuthScope("hostname", port, AuthScope.ANY_REALM), defaultcreds);
try {
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
throw new Exception("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
return new String(responseBody);
}
catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
throw e;
}
catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
throw e;
}
finally {
// Release the connection.
method.releaseConnection();
}
异常文本如下所示:
Fatal transport error: chunked stream ended unexpectedly Exception in thread "main" java.io.IOException: chunked stream ended unexpectedly at org.apache.commons.httpclient.ChunkedInputStream.getChunkSizeFromInputStream(ChunkedInputStream.java:252) at org.apache.commons.httpclient.ChunkedInputStream.nextChunk(ChunkedInputStream.java:221) at org.apache.commons.httpclient.ChunkedInputStream.read(ChunkedInputStream.java:176) at java.io.FilterInputStream.read(FilterInputStream.java:127) at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:108) at java.io.FilterInputStream.read(FilterInputStream.java:101) at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:127) at org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:690)
无论是使用getResponseBody()还是getResponseBodyAsStream(),我都会遇到类似的异常。
我不应该收回太多数据,但是我发布了超过200mb的数据。
答案 0 :(得分:1)
我可以通过更改PostMethod的requestHeader中指定的文件名值的长度来解决这个问题。我在请求标头中包含了完整文件路径名的编码版本。通过反复试验,我发现我“发布”文件的成功或失败似乎取决于它所在的文件夹。长文件夹文件路径名不起作用,而短文件路径名虽然使用相同的文件,但却无效。所以我从请求标题中删除了路径名,只开始包含文件名,我不再看到问题了。
答案 1 :(得分:0)
可能很旧,可以节省一些时间..... 我在Server是Python且Clinet是Java的地方遇到了这个错误。
第一-
来自Java客户端的错误“通过http java.io.IOException发送数据时出错:在块末尾预期出现CRLF:79/82 java.io.IOException:块末尾应为CRLF:79/82“
第二-
来自Java Clinet的错误“通过http java.io.IOException发送数据时出错:分块流意外结束 java.io.IOException:分块的流意外结束“
通过更改分块流大小的确定响应,两种错误都得到解决
有问题的人-
HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\nServer: Jetty(6.1.26)\r\n\r\nDE\r\n
解决--
HTTP/1.1 200 OK\r\nContent-Length: 20000\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\nServer: Jetty(6.1.26)\r\n\r\n229\r\n
注意= nDE替换为n229