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.
File upload requests over HTTP are usually sent using
multipart/form-datarequest encoding and not usingapplication/x-www-form-urlencodedencoding as you seem to expect with yourgetParameter()attempt. ThegetInputStream()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.Or if you’re still on Servlet 2.5 or even older, then grab Apache Commons FileUpload.
See also:
multipart/form-datarequest in a servlet