HttpClient, problem for doing POST multithreaded in Chunked mode ...

Well, I wonder how I can achieve to publish multi-page mode. I have 3 parts, and files that can be large should be sent to pieces.

That's what I'm doing:

MultipartEntity multipartEntity = new MultipartEntity() { @Override public boolean isChunked() { return true; } }; multipartEntity.addPart("theText", new StringBody("some text", Charset.forName("UTF-8"))); FileBody fileBody1 = new FileBody(file1); multipartEntity.addPart("theFile1", fileBody1); FileBody fileBody2 = new FileBody(file2); multipartEntity.addPart("theFile2", fileBody2); httppost.setEntity(multipartEntity); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpClient httpClient = new DefaultHttpClient(params); HttpResponse httpResponse = httpClient.execute(httppost); 

On the server side, I get 3 parts, but the files, for example, are not divided, they are accepted as one part ... basically I see only 4 borders: 3 --xxx, 1 at the end --xxx--. I thought that overriding isChunked would do the trick, but no ...; (

Am I trying to do what I do? How could I do this job?

Many thanks. Fab

+1
file multipart chunked
source share
2 answers

To create a multi-page piece, one of the parts must have a size that is not available. Like the part that is being broadcast.

For example, suppose your file2 is a really big video. You can replace part of your code:

 FileBody fileBody2 = new FileBody(file2); multipartEntity.addPart("theFile2", fileBody2); 

with this code:

 final InputStreamBody binVideo = new InputStreamBody(new FileInputStream(file2), "video/mp4", file2.getName()); multipartEntity.addPart("video", binVideo); 

since now the third part is an InputStream instead of a File, your multi-page HTTP request will have the Transfer-Encoding: chunked header.

+2
source

Usually, any suitable server-side server infrastructure (such as the Java EE Servlet API) will hide transport data, such as transfer encoding from application code. just because you don’t see fragment delimiters by reading from the content stream does not mean that the fragment encoding was not used by the basic HTTP transport.

You can see exactly what HTTP packets HttpClient generates by activating wiring, as described here:

http://hc.apache.org/httpcomponents-client-ga/logging.html

0
source

All Articles