1

この問題は、投稿しようとしている「大きな」ファイルでのみ発生するようです。

私のコードは次のようになります。

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() のどちらを使用しても同様の例外が発生します。

あまり多くのデータが返ってこないはずですが、200 MB を超えるデータを投稿しています。

4

2 に答える 2

1

PostMethod の requestHeader で指定されたファイル名の値の長さを変更することで、この問題を解決できます。リクエストヘッダーに完全なファイルパス名のエンコードされたバージョンを含めていました。試行錯誤の結果、「投稿」していたファイルの成功または失敗は、それがあったフォルダーに依存しているように見えることがわかりました。長いフォルダーファイルパス名は機能しませんでしたが、同じファイルではありますが短いパス名は機能していました。そのため、リクエストヘッダーからパス名を削除し、ファイル名のみを含め始めたので、問題は発生しなくなりました。

于 2013-02-26T16:59:00.767 に答える