0

How can I read a JPEG image in Servlet when I resize&upload it using Flash?

I have it in my doPost() method, but I cannot find the file. I cannot get it by request.getParameter(...).

When I get it with request.getInputStream() and write it to a file, then I cannot open it. Somehow the JPEG encoding is corrupted.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
lembas
  • 377
  • 1
  • 7
  • 21

1 Answers1

0

File upload requests over HTTP are usually sent using multipart/form-data request encoding and not using application/x-www-form-urlencoded encoding as you seem to expect with your getParameter() attempt. The getInputStream() gives you the entire request body, which is also not what you want. You basically need to parse it into useable parts and extract the uploaded file from it.

If you're already on Servlet 3.0 (which is already out for almost 3 years), then just use getPart() instead.

Part uploadedFile = request.getPart("fieldName");
InputStream uploadedFileContent = uploadedFile.getInputStream();
// Now just write it to an arbitrary OutputStream the usual way.

Or if you're still on Servlet 2.5 or even older, then grab Apache Commons FileUpload.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555