0

I'm getting back data (an image) from my database. It has a header which looks like this below and also a footer that says webkitboundry.

------WebKitFormBoundaryv7RHmVQhhWVAEycr
Content-Disposition: form-data; name="file"; filename="hey.jpg"
Content-Type: image/jpeg

image IO in java does not recognize this as a valid image because of the header and footer. I am sending this to my database like so:

xmlhttp.open("post","http://localhost:8080/restService/api/submitinfo",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(formData);

is there any way I can send this file without the header or how can I go about removing it? I know I might be able to strip it with some for loops but I'm curious that there might be another way. What do you professionals do with this?

2
  • 1
    Use a multipart MIME decoder. Commented Dec 26, 2013 at 20:56
  • 3
    It's likely that the image data is also in base64 encoding, so a ImageIo won't read that either. The short answer is, no, this is generally how data is transmitted on the web Commented Dec 26, 2013 at 21:05

2 Answers 2

1

Your server-side component should process the form post and extract the binary image data before storing the image in the database.

Have a look at Apache Commons FileUpload.

Sign up to request clarification or add additional context in comments.

2 Comments

This is actually a great answer got everything working because of your post. Thanks.
Found an easier way with jersey. answered below.
1

Found an easy way to separate header from file data reading up on the jersey docs. When uploading files.

public String post(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) {

I was not including FormDataContentDisposition which separates the header information from the file. Which made it possable for me to use the data corectly!

Hope someone who needs this finds it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.