-1

I don't have the code available right now, but I'll describe my situation and post code later on :). Basically, my problem is with uploading image to server via POST (from Java desktop application / client). I found some discussions here on this matter, but it either included using org.apache.commons.httpclient classes, and I have most of the code implemented using URLConnection class, OR with bugs in it. Check this selected answer to see what I followed in as much detail as possible: Using java.net.URLConnection to fire and handle HTTP requests

Anyways, I created php script on my local server that handles form with file upload. I checked it from one static html that was created along with .php. I then checked it without file upload, and it works ok. It also works ok when I send plain text file. This looks like a problem related to encoding. Maybe some strange character is breaking my transfer?

As I said, I'll include code if necessary later today, but I'm interested in your opinions / experience related to encoding images when sending via POST. Should I do something else, that's not being mentioned in the link above.

QUESTION: do I need to use some kind of encoding to actual data when sending binary file in multipart message via http post?

Sorry for no code right now.

Cheers

Community
  • 1
  • 1
hummingBird
  • 2,235
  • 3
  • 21
  • 40

2 Answers2

1

As for your question: yes, you need to encode the file.

I would use HttpClient for this as it takes care of all those things for you. No need to reinvent the wheel. See How to upload a file using Java HttpClient library working with PHP for code.

Community
  • 1
  • 1
pap
  • 25,288
  • 6
  • 37
  • 46
  • Thank you... I'll probably use HttpClient in the end. However, I spent quite some time trying to debug this, and would really like to sort this out. The fact that plain text files are being transferred ok suggests that it's a matter of encoding. – hummingBird Aug 01 '11 at 11:45
1

If you are using URLConnection to handle multipart/form-data manually, I guess you you need to add "Content-Transfer-Encoding: binary" to the image part. See W3C's docs.

Eduardo Costa
  • 1,894
  • 1
  • 16
  • 22
  • Unfortunately, this isn't the fix I was looking for, as it's listed in the link I posted :(. Thanks, anyway. – hummingBird Aug 01 '11 at 11:49
  • 1
    Uh, sorry, it was hidden by a scrollbar... :) BTW, did you tested with IE, Firefox, Chrome, Safari? – Eduardo Costa Aug 01 '11 at 21:29
  • Interestingly enough, no :). I was actually concentrated on the fact that this thing worked with plain text, so I figured it has something to do with binary content. – hummingBird Aug 02 '11 at 07:05