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

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

问题描述

我很难用我的 Java 应用程序服务器处理多部分/表单数据请求.据我所知,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 中处理多部分/表单数据?

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 中处理多部分/表单数据 POST 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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