0

I am uploading a file through multipartEntityBuilder in java. file get uploaded but gets corrupted, as content header gets mixed with data at file.

getting error in text and image formats working fine for pdf.

HttpClient httpclient =new HttpClient();
HttpPut post = new HttpPut(uploadfileurl);
File file = new File(fileUrl);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();         
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, "test.txt");

builder.addTextBody("text", message, ContentType.TEXT_PLAIN);
HttpEntity entity = builder.build();
post.setEntity(entity);
post.setHeader("enctype", "multipart/form-data");
HttpResponse httpresponse =  httpclient.execute(post);
HttpEntity resEntity = httpresponse.getEntity();

Error in file :: this should be like this :

this file is for testing

but it going like this :

---------------1427465571114 Content-Disposition: form-data; name="upfile"; filename="" Content-Type: application/octet-stream

this file is for testing

---------------1427465571114--

Danielson
  • 2,605
  • 2
  • 28
  • 51
Abhi
  • 9
  • 1
  • 4
  • Code looks all fine. Your problem is in servlet. You're incorrectly grabbibng the file by `request.getInputStream()` or `request.getReader()` instead of `request.getPart()`. Duplicate answers in detail how to deal with those kind of requests in servlet. – BalusC Mar 03 '16 at 13:35

2 Answers2

2

well, actually it's not corrupted. That is the right http post request. if you want to get the content of the file, have you tried this method

httpresponse.getEntity().getContent()

it will return InputStream object in which you can try to read the content.

kucing_terbang
  • 4,991
  • 2
  • 22
  • 28
  • this is the content of the file where it upload. I want to upload the file but file that gets uploads has this header information as the contents of the file. the starting and ending content is including this header info, – Abhi Mar 29 '15 at 11:00
  • i afraid i don't get what you mean. if you want to upload the file that it should be fine. – kucing_terbang Mar 29 '15 at 11:10
  • the text content in file is not the same. -142746557********application/octet-stream this text is added there in the file – Abhi Mar 29 '15 at 12:18
0

(BTW i am using Zip4J if anyone wonders about my zip.getFile() calls)

As the name suggests : It's to pass a multipart requests.

Here is a snippet of a code to build a multipart header for 2 files :

MultipartEntityBuilder mpeBuilder = MultipartEntityBuilder.create();
        mpeBuilder.addBinaryBody(zip.getFile().getName(), zip.getFile());
        mpeBuilder.addBinaryBody(zip.getFile().getName(), zip.getFile());

        post.setEntity(mpeBuilder.build());

I can then find a file with the following content on my server :

 --d_sc7jYe4LHAMikc1LbDw59Yz3pz_bn
Content-Disposition: form-data; name="temp.zip"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

BINARY GARBAGE
--d_sc7jYe4LHAMikc1LbDw59Yz3pz_bn
Content-Disposition: form-data; name="temp.zip"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

BINARY GARBAGE
--d_sc7jYe4LHAMikc1LbDw59Yz3pz_bn--

As you can imagine these headers and separators are here in order to separate and identify the data.

So, I would say this is not garbage, you need to handle this on server side, if you want, say, to have the 2 files saved in the right way.

If you just want to upload a file, there is, as the documentation suggests a fileEntity :

 post.setEntity(new FileEntity(zip.getFile()));

By using this entity my zip file is sent to the server without any "corruption"

https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

Hello_world
  • 303
  • 2
  • 11