I can't use request as there are some flaws in multipart implementations.
Then your actual problem is that you're using a poor implementation. The multipart/form-data
request encoding isn't by default supported by the Servlet API, you won't see anything in the request parameter map. To retrieve the uploaded file and the other request parameters, you need to parse the InputStream
of the HttpServletRequest
yourself. But fortunately there's a commonly used API which can take the precious and tedious work from your hands: Apache Commons FileUpload. At their homepage you can find lot of code examples and important tips&tricks in the User Guide and Frequently Asked Questions sections. Read them carefully. Basically you just need to get the InputStream
from the FileItem
object and write it to any OutputStream
to your taste using the usual Java IO way. You can even use the shorthand FileItem#write()
for this.
You can if necessary also write a Filter
which makes use of Apache Commons FileUpload under the hood and checks every request if it is multipart/form-data
and if so, then put the parameters back in the request parameter map with help of Commons FileUpload and put the uploaded files (or exceptions) as request attributes, so that it's finally a bit more transparently in your servlet code. You can find here a basic example to get the idea.
Summarized: don't invent workarounds/hacks to "fix" this problem. For sure don't use Ajax to set request parameters in the session while they are sent to the server side at any way. Just use the right solution for the problem. Use a good multipart/form-data
parser.
Hope this helps.