0

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);
Vipin Nemade
  • 115
  • 1
  • 5
  • 12
  • Your local file gets changed when you upload it? Or is this what arrives on the server? – flup Jan 16 '13 at 09:46

2 Answers2

0

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

You're doing a PUT request, yet your client uses multipart encoding as commonly uses in HTML form posts.

Julian Reschke
  • 40,156
  • 8
  • 95
  • 98
  • AFAIK, there is nothing in the HTTP spec that says that you can't do this in a PUT request. On the contrary, the spec says it is the responsibility of the server to either handle a multipart content-type properly ... or reject the PUT request. (However, there isn't watertight evidence that a proper content-type *is* being sent ...) – Stephen C Jan 16 '13 at 10:27
  • That is true. But there is also nothing in the spec that says that a server should do anything except storing the payload (which it did). – Julian Reschke Jan 16 '13 at 10:38
  • I am uploading file on swift storage – Vipin Nemade Jan 16 '13 at 10:42
  • @JulianReschke - one *could* read the spec that way. However, that behaviour is so obviously unhelpful and pointless that any server that did that is (IMO) broken. – Stephen C Jan 16 '13 at 11:41
  • Stephen: disagreed. This is what most servers do with PUT. If you don't want the enclosure, just don't send it. – Julian Reschke Jan 16 '13 at 12:41
  • I believe HttpClient's FileEntity is what you need to use. – Julian Reschke Jan 17 '13 at 08:45