I am uploading file using httpclient. After uploading file size get changed. During file upload some extra things get added in to file.
Before uploading file it contains:
hi this is vipin check
After uploading the file contains:
--j9q7PmvnWSP9wKHHp2w_KCI4Q2jCniJvPbrE0
Content-Disposition: form-data; name="vipin.txt"; filename="vipin.txt"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
hi this is vipin check
--j9q7PmvnWSP9wKHHp2w_KCI4Q2jCniJvPbrE0--
Why file size is changing?
Why does this extra contents get added?
My httpclient code is:
HttpPut httppost = new HttpPut(URIUtil.encodeQuery(newUrl));
httppost.setHeader("X-Auth-Token", cred.getAuthToken());
httppost.addHeader("User-Agent", "NetMagic-file-upload");
System.out.println("Dest : " + dest.getAbsolutePath());
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = (ContentBody) new FileBody(src);
mpEntity.addPart(dest.getName(), cbFile);
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
You're doing a PUT request, yet your client uses multipart encoding as commonly uses in HTML form posts.
What appears to be happening is that the client is sending the file to be uploaded as multipart entity, but the server is treating it as an plain file. It is not entirely clear where the fault lies.
It is possible that the server is ignoring the content type in the request header. That would most likely be a bug in the servlet (or whatever) that is responsible for handing the upload request.
It is possible that the client is not setting a content type in the request header. I'd have expected that the client library would take care of that for you. But it is possible that you need to do it explicitly.
I'd advise looking at the request headers as they are sent by the client or received by the server to see if there is a proper multi-part content-type. That will help you determine where the problem is.
But there is an obvious solution. If the server cannot cope with multiparts, change the client side to not send them.
Related
I'm trying to send a file with resteasy client to an http server with some code like this:
File source = new File("test.pdf");
Client client = ClientBuilder.newClient();
MultipartFormDataOutput upload = new MultipartFormDataOutput();
upload.addFormData("source", source, MediaType.APPLICATION_OCTET_STREAM_TYPE);
Entity entity = Entity.entity(upload, MediaType.MULTIPART_FORM_DATA_TYPE)
Response response = client.target(url).request().post(entity);
What happens is that on the http server I'm not getting the usual "file" in request (with the content, the name etc..), but something like a regular POST parameter named "source" with the file content as its value.
I tried it with some different web servers, so the issue have to be in the request that RESTeasy builds.
Any help?
MultipartFormDataOutput behaves the same away as a HTML form would do. It sends key/value pairs to the server.
If you want to upload a MIME message consider using MultipartOutput.
I am building an application in Netty which will allow people to download and upload file. But before that they should be able to log into the server. Once they log in they can download or upload the file.
Issue I am having is how will I know the data I have received is a string(username password string) or a chunked file(as I send files as ChunkedFile) or any other java object. How will I properly get them to original form. I know we can use encoder and decoders but there are no proper example available which shows anything close to the problem I am having?
Thanks
Send a POST http request with Headers
// Prepare the HTTP request.
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriJson.toASCIIString());
request.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
ByteBuf buf = Unpooled.copiedBuffer(data, CharsetUtil.UTF_8);
request.content().writeBytes(buf);
request.headers().set(CONTENT_LENGTH,request.content().readableBytes());
This question already has answers here:
How can I upload files to a server using JSP/Servlet?
(14 answers)
Closed 6 years ago.
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--
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.
(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
For my current solution, I'm using apache commons FileUpload library to process incoming multi-part requests. I'm able to send the files appropriately then read the stream on the server end using the streaming api code here.
If you look at the format of multipart requests listed here, there is a Content-Disposition listed for each file added. I need to add a startByte tag (similar to how you add a "filename" tag in content-disposition). I'm not too sure how to do that properly and then retrieve it in the request? This is of course not a global header, because multiple files are in this stream.
Anyone have any ideas?
This is for anyone who might be interested, and turns out was easy: To do this, on the client, you append the header like below:
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + filename + "\"; filename=\"" + filename + "\";\r\n");
outputStream.writeBytes("My-Custom-Header: My-Data\r\n");
outputStream.writeBytes("\r\n");
Then, on the server, using commons FileUpload, you would just do:
FileItemHeaders headers = item.getHeaders();
headers.getHeader("My-Custom-Header");
i am creating a desktop application which send file to an tomcat server. the servlet receiver and saves file fine.
I need some help to do a java program that post in a https site. I dont know how to put the parameters because it a multpart form data contect type.. Please help! when I do a post with firefox its like this...
This will depend. I've used the following technique to upload a multi-part file to a server before, based on providing a series of form key/name pairs.
This will be depended on you own requirements and what the servlet is actually expecting...
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
String name = file.getName();
entity.addPart(new FormBodyPart("someFormParameter", new StringBody("someFormName")));
/*...*/
entity.addPart("formFileNameParameter", new FileBody(file, mimeType));
HttpClient client = /*...*/
HttpPost post = new HttpPost(url.toURI());
post.setEntity(entity);
HttpResponse response = client.execute(post);
// Process response