Java 从多部分请求中获取表单参数而不获取文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1748259/
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
Get form parameters from multipart request without getting the files
提问by pvgoddijn
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).
我正在寻找一种方法来获取 Servlet 过滤器中 HTTP 多部分请求的表单参数,而无需上传文件(还)。
request.getParameterMap() returns empty. I understand this is because of the request being multi-part.
request.getParameterMap() 返回空。我理解这是因为请求是多部分的。
I've looked at commons.HttpFileUpload but this seems to be overkill for my situation. In this filter I'm only interested in the normal parameters, and don't want to handle the file-upload yet.
我看过 commons.HttpFileUpload 但这对我的情况来说似乎有点过分了。在这个过滤器中,我只对普通参数感兴趣,还不想处理文件上传。
Edit: the main problem is that I need to have an intact HttpRequestObject further down the filter stack. The HttpFileUpload seems to consume part of the request data (probably by using the data stream object and closing it again.)
编辑:主要问题是我需要在过滤器堆栈的下方有一个完整的 HttpRequestObject 。HttpFileUpload 似乎消耗了部分请求数据(可能通过使用数据流对象并再次关闭它。)
采纳答案by BalusC
It's certainly not overkill, it's the right way and always better than writing the parser yourself. The Apache Commons FileUploadis 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 specificationand start with reading the HttpServletRequest#getInputStream()
(warning: this is a mix of binary and character data!).
这当然不是矫枉过正,这是正确的方法,并且总是比自己编写解析器更好。在Apache的通用FileUpload开发并维护多年,证明了其在处理多/表单数据请求的鲁棒性。你不想重新发明轮子。如果您真的想这样做(我不推荐这样做),请阅读multipart/form-data 规范并开始阅读HttpServletRequest#getInputStream()
(警告:这是二进制和字符数据的混合!)。
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 herea basic example to get the idea.
如有必要,您还可以编写一个过滤器,它在后台使用 Apache Commons FileUpload 并检查每个请求是否是多部分/表单数据,如果是,则在 Commons FileUpload 的帮助下将参数放回请求参数映射中,将上传的文件(或异常)作为请求属性,以便它最终在您的 servlet 代码中更加透明。你可以在这里找到一个基本的例子来了解这个想法。
Hope this helps.
希望这可以帮助。
回答by Tendayi Mawushe
The Oreilly Servletswebsite has some sample code which you can download customise and use. This includes MultipartRequestwhich sounds like it does what you require, it un-boxes a multipart request and allows access to the parameters and the files separately.
该奥赖利Servlet的网站提供了一些示例代码,你可以下载定制和使用。这包括MultipartRequest,听起来它可以满足您的要求,它将多部分请求拆箱并允许分别访问参数和文件。
回答by Ed .
Just to add to the answers already provided - I had a very similar problem in that I was trying to add some CSRF validation to our existing web app. We decided to include a special token in each form using some JS and add a servlet filter to check that the token existed (therefore a generic, isolated solution).
只是添加到已经提供的答案中 - 我有一个非常相似的问题,因为我试图向我们现有的 Web 应用程序添加一些 CSRF 验证。我们决定使用一些 JS 在每个表单中包含一个特殊的令牌,并添加一个 servlet 过滤器来检查令牌是否存在(因此是一个通用的、隔离的解决方案)。
The servlet would check if the token was present but broke for every form that provided a file upload option. Hence I landed at this page frequently while doing some googling.
servlet 将检查令牌是否存在,但对于提供文件上传选项的每个表单都会中断。因此,我在谷歌搜索时经常登陆这个页面。
The work around we used (while attempting to avoid any dealings with the uploaded files) was to get some JavaScript to add the token as a GET parameter, i.e. We modified the form's action URL to include the token and therefore could use the HttpServletRequest.getParameter() method for the token (and only the token).
我们使用的解决方法(同时试图避免处理上传的文件)是让一些 JavaScript 将令牌添加为 GET 参数,即我们修改了表单的操作 URL 以包含令牌,因此可以使用 HttpServletRequest.getParameter () 方法用于令牌(并且只有令牌)。
I have tested this in IE, FF and Chrome and all seem to be happy.
我已经在 IE、FF 和 Chrome 中对此进行了测试,似乎都很满意。
Hope this helps anyone who also finds themselves in a similar situation.
希望这可以帮助任何也发现自己处于类似情况的人。
回答by Christopher Johnson
Commons FileUpload provides a mechanism to read request params from a multipart form upload.
Commons FileUpload 提供了一种从多部分表单上传中读取请求参数的机制。
There's a really great example of how to grab the request parameters here:
这里有一个非常好的示例,说明如何获取请求参数: