2017-08-17 58 views
0

我正在使用Java與HttpAsyncClient,並嘗試使用multipart/form向服務器發送請求。有兩個參數:一個只是一個字符串,而另一個是fie/byte數組。當我需要使用大容量字節數組執行請求時,出現以下異常:org.apache.http.ContentTooLongException:內容長度過長。MultipartFormEntity - 內容太長

有什麼辦法可以解決這個問題嗎?我怎麼能使用Java和這個實體進行多部分/形式的大規模請求?

下面是我的一些代碼:

final HttpPost post = new HttpPost(uri); 
final HttpEntity entity = MultipartEntityBuilder.create() 
       .addTextBody("name", fileName).addBinaryBody("file", rawContent, ContentType.APPLICATION_OCTET_STREAM, fileName) 
       .build(); 
post.setEntity(entity); 
return client.execute(post, null); 

哪裏rawContent只是一個字節數組。

回答

0

回答這個問題'有什麼辦法可以解決這個問題嗎? ': 你可以試試這個代碼在你的‘MultipartFormEntity’類

@Override 
    public InputStream getContent() throws IOException { 
     if (this.contentLength < 0) { 
      throw new ContentTooLongException("Content length is unknown"); 
     } else if (this.contentLength > 25 * 1024) { 
      throw new ContentTooLongException("Content length is too long: " + this.contentLength); 
     } 
     final ByteArrayOutputStream outstream = new ByteArrayOutputStream(); 
     writeTo(outstream); 
     outstream.flush(); 
     return new ByteArrayInputStream(outstream.toByteArray()); 
    } 

    @Override 
    public void writeTo(final OutputStream outstream) throws IOException { 
     this.multipart.writeTo(outstream); 
    } 

} 
+0

MultipartFormEntity是不是我的課,它與HTTP客戶端使用了Apache類。我很確定你剛發佈的代碼已經存在了。 – baki1995