如何在我的java servlet中处理multipart / form-data POST请求? [英] How can I handle multipart/form-data POST requests in my java servlet?

查看:2091
本文介绍了如何在我的java servlet中处理multipart / form-data POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难用我的java应用服务器处理multipart / form-data请求。根据我的发现,servlet 3.0规范提供了诸如HttpServletRequest.getParts()之类的方法,它非常适合处理上传到我的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.

但是,此方法是3.0 servlet规范的一部分,我的应用程序服务器(Tomcat 6)尚不支持此方法。即使使用有效的3.0 web.xml文件和java EE 6库,我在尝试调用getParts()时也会遇到以下异常:

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;

切换应用程序服务器对于这个项目来说并不是一个可行的选择。是否有任何第三方库可用于处理java servlet中的multipart / form-data?

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?

推荐答案

签出 Apache Commons Fileupload 。它为您提供了一个编程API,用于解析多部分请求,并逐个遍历它的各个部分。

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.

这篇关于如何在我的java servlet中处理multipart / form-data POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆