Get form parameters from multipart request without getting the files

后端 未结 4 1009
暗喜
暗喜 2020-12-10 20:55

I\'m looking for a way to get the form parameters of a HTTP multi-part request in a Servlet-filter without uploading files (yet).

request.getParameterMap() returns e

4条回答
  •  自闭症患者
    2020-12-10 21:22

    It's certainly not overkill, it's the right way and always better than writing the parser yourself. The Apache Commons FileUpload is developed and maintained for years and has proven its robustness in handling multipart/form-data requests. You don't want to reinvent the wheel. If you really want to do it (I don't recommend it), then read on the multipart/form-data specification and start with reading the HttpServletRequest#getInputStream() (warning: this is a mix of binary and character data!).

    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.

    Hope this helps.

提交回复
热议问题