如何在我的 java servlet 中处理多部分/表单数据 POST 请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3510788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:10:23  来源:igfitidea点击:

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

javaservletsmultipartform-data

提问by Nik Reiman

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.

我很难用我的 Java 应用程序服务器处理多部分/表单数据请求。据我所知,servlet 3.0 规范提供了诸如 HttpServletRequest.getParts() 之类的方法,它非常适合处理上传到我的 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():

但是,此方法是 3.0 servlet 规范的一部分,我的应用程序服务器(Tomcat 6)尚不支持此方法。即使使用有效的 3.0 web.xml 文件和 java EE 6 库,在尝试调用 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?

对于这个项目来说,切换应用服务器并不是一个真正可行的选择。是否有任何第三方库可用于在 java servlet 中处理多部分/表单数据?

回答by Andrzej Doyle

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

查看Apache Commons Fileupload。它为您提供了一个编程 API 来解析多部分请求,并单独遍历它的各个部分。

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

我过去曾使用它进行简单的多部分处理,它可以很好地完成工作而不会过于复杂。

回答by Vineet Reynolds

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.

Tomcat 6 不支持也不会支持 Servlet Specification 3.0。您应该尝试在 Tomcat 7 上执行此操作,但我不确定当前可用的 beta 版本中是否存在此功能。不过,预计该功能将出现在生产版本中。

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).

您可以继续使用 Apache Commons FileUpload,就像在其他答案中发布的那样,或者您可以使用 Glassfish(取决于项目的当前阶段和类型)。

回答by Divyesh Rupawala

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.

当我们使用 post 方法时数据被加密,所以我们必须使用 servletfileupload 来获取请求的数据,使用 FileItemIterator 我们可以获得所有表单数据。

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

我已经在这个链接上回答了如何处理在 servlet 中发送的 Google Web Toolkit 的表单