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: