为什么当我使用HttpClient时,我得到的分块流意外结束?

问题描述 投票:0回答:2

[该问题似乎只与我要发布的“大”文件有关。

我的代码如下:

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

异常文本看起来像这样:

致命的传输错误:分块的流意外结束线程“主”中的异常java.io.IOException:分块的流意外结束在org.apache.commons.httpclient.ChunkedInputStream.getChunkSizeFromInputStream(ChunkedInputStream.java:252)在org.apache.commons.httpclient.ChunkedInputStream.nextChunk(ChunkedInputStream.java:221)在org.apache.commons.httpclient.ChunkedInputStream.read(ChunkedInputStream.java:176)在java.io.FilterInputStream.read(FilterInputStream.java:127)在org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:108)在java.io.FilterInputStream.read(FilterInputStream.java:101)在org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:127)在org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:690)

无论我使用getResponseBody()还是getResponseBodyAsStream(),我都会收到类似的异常。

我不应该获取太多数据,但是我要发布200mb以上的数据。

java apache-commons-httpclient
2个回答
1
投票

我能够通过更改PostMethod的requestHeader中指定的文件名值的长度来解决此问题。我一直在请求标头中包含完整文件路径名的编码版本。通过反复试验,我发现我正在“发布”的文件的成功或失败似乎取决于它所在的文件夹。一个长文件夹的文件路径名不起作用,而一个短文件夹的文件名却起作用。因此,我从请求标头中删除了路径名,只开始包含文件名,而我不再看到问题了。


0
投票

可能很旧,可以节省一些时间.....我在服务器位于Python且Clinet为Java的位置出现此错误。

1st-来自Java客户端的错误“通过http java.io.IOException发送数据时出错:在块末尾应为CRLF:79/82java.io.IOException:块末尾应为CRLF:79/82“

来自Java Clinet的第二错误-通过HTTP java.io.IOException发送数据时出错:分块流意外结束java.io.IOException:分块的流意外结束“

两个错误都通过更改带有分块流大小的确定响应得到解决

一个有问题-“ HTTP / 1.1 200 OK \ r \ n内容类型:application / json \ r \ n传输编码:成块\ r \ n服务器: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

© www.soinside.com 2019 - 2024. All rights reserved.