简体   繁体   中英

How can I handle multipart/form-data POST requests in my java servlet?

I'm having a very hard time dealing with multipart/form-data requests with my java application server. From what I have found out, the servlet 3.0 specification provides methods such as HttpServletRequest.getParts(), which would be ideal for processing the form data uploaded to my servlet.

However, this method is part of the 3.0 servlet specification, and my application server (Tomcat 6) does not support this yet. Even with a valid 3.0 web.xml file and the java EE 6 libs, I get the following exception when trying to call getParts():

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getParts()Ljava/util/Collection;

Switching application servers is not really a feasible option for this project. Are there any third-party libraries available for processing multipart/form-data within java servlets?

Check out Apache Commons Fileupload . It gives you a programmatic API to parse a multipart request, and iterate through the parts of it individually.

I've used it in the past for straightforward multipart processing and it does the job fine without being overly complicated.

Tomcat 6 does not and will not support Servlet Specification 3.0 . You should attempt doing this on Tomcat 7, but I'm not really sure whether this functionality is present in the beta release that is currently available. The functionality is expected to be present in the production release though.

You could continue using Apache Commons FileUpload like posted in the other answer, or you could use Glassfish (depending on the current phase and type of your project).

when we used post method than data are encrypted so we have to used servletfileupload to get requested data and using FileItemIterator we can get all form data.

i already answer on this link How to process a form sent Google Web Toolkit in a servlet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM