java 带有 enctype="multipart/form-data" 的表单是否会导致访问隐藏字段的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2827778/
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-29 23:01:51  来源:igfitidea点击:

Does form with enctype="multipart/form-data" cause problems accessing a hidden field

javahtmlformsservletshidden-field

提问by Ankur

I have created a hidden form element

我创建了一个隐藏的表单元素

<form name="UploadImage" enctype="multipart/form-data" method="post" action="UploadImage">
    <label>
        </label>
    <input name="imgUploadObjId" id="imgUploadObjId" value="52" type="hidden">

    //rest of the form here

</form>

And I am trying to get the value with this line in a servlet (as I have done before):

我正在尝试使用 servlet 中的这一行获取值(正如我之前所做的那样):

int objId = Integer.parseInt(request.getParameter("imgUploadObjId"));

But I get this (line 33 is the line above):

但我明白了(第 33 行是上面的行):

java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source) java.lang.Integer.parseInt(Unknown Source) web.objects.UploadImage.doPost(UploadImage.java:33) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source) java.lang.Integer.parseInt(Unknown Source) web.objects.UploadImage.doPost(UploadImage.java:33) javax.servlet.http.HttpServlet .service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Is there something different about a form with enctype="multipart/form-data"? Or can you see some other error.

带有 enctype="multipart/form-data" 的表单有什么不同吗?或者你能看到其他一些错误。

回答by BalusC

The servlet parses the parameters by default using application/x-www-form-urlencodedencoding. The multipart/form-dataencoding however isn't supported in servlets until Servlet 3.0. The getParameter()calls will all return null.

默认情况下,servlet 使用application/x-www-form-urlencoded编码解析参数。该multipart/form-data编码但是不支持的servlet直到Servlet的3.0。该getParameter()调用将都返回null

In Servlet 3.0, you should have used HttpServletRequest#getParts()instead to get all parts of a multipart/form-datarequest, including normal form fields. Prior to Servlet 3.0, you should have used Apache Commons FileUploadto parse multipart/form-datarequests. See also the following answer for a detailed example of both approaches: How to upload files to server using JSP/Servlet?

在 Servlet 3.0 中,您应该HttpServletRequest#getParts()改为使用来获取multipart/form-data请求的所有部分,包括普通表单字段。在 Servlet 3.0 之前,您应该已经使用Apache Commons FileUpload来解析multipart/form-data请求。有关这两种方法的详细示例,另请参阅以下答案:如何使用 JSP/Servlet 将文件上传到服务器?

Note that if you aren't using any <input type="file">field at all, then you can just leave the encoding away from the <form>. It will then default to application/x-www-form-urlencoded.

请注意,如果您根本不使用任何<input type="file">字段,那么您可以将编码从<form>. 然后它将默认为application/x-www-form-urlencoded.

回答by Gabriel Belingueres

As a workaround, you can also add the required hidden parameters as GET parameters in the form's action attribute:

作为一种解决方法,您还可以在表单的 action 属性中添加所需的隐藏参数作为 GET 参数:

<form name="UploadImage" enctype="multipart/form-data" method="post" action="UploadImage?imgUploadObjId=52">

    //rest of the form here

</form>

this will allow the request.getParameter("imgUploadObjId")call to work.

这将允许request.getParameter("imgUploadObjId")呼叫工作。

回答by Alexander Pogrebnyak

Indeed there is something different.

确实有些不同。

request.getParameterwill only work for hardcoded URL parameters specified in actionattribute of <form>element. In your case it does not contain any.

request.getParameter仅适用于元素action属性中指定的硬编码 URL 参数<form>。在您的情况下,它不包含任何内容。

All other parameters will be incoded into the form itself, which you have to process by parsing HTTP request's input stream directly.

所有其他参数都将被编码到表单本身中,您必须通过直接解析 HTTP 请求的输入流来处理它。

Fortunately, you are not the first and there are some good open-source libraries that take care of this.

幸运的是,您不是第一个,并且有一些很好的开源库可以解决这个问题。

I've been using Apache FileUpload. You create a parser and pass a request object to it and then iterate through different items. One of them will be your hidden field.

我一直在使用Apache FileUpload。您创建一个解析器并将请求对象传递给它,然后遍历不同的项目。其中之一将是您的隐藏字段。

回答by Steven P.

The multi-part encoding shouldn't affect hidden text fields. It is likely something else. Can you post more of the HTML/Servlet code?

多部分编码不应影响隐藏的文本字段。这很可能是别的东西。你能发布更多的 HTML/Servlet 代码吗?

回答by Gala101

Not sure if this helps, but I have used multipart forms in jsp pages which are submitted to a struts servlet and these pages have hidden fields which are received in my Struts Action classes (wrapped in Struts ActionForm), so I don't think there is any hard stop here.

不确定这是否有帮助,但我在提交给 struts servlet 的 jsp 页面中使用了多部分表单,并且这些页面具有在我的 Struts Action 类中接收的隐藏字段(包装在 Struts ActionForm 中),所以我认为没有在这里有什么困难。

Have you tried receiving this value as String and seeing what actually comes there?

您是否尝试过将此值作为字符串接收并查看实际出现的内容?

回答by Alfabravo

You'd check the servlet code itself. Are you getting the request? Can you debug the app in order to see which variables are present in the environment when you try to get the value and parse it.

您将检查 servlet 代码本身。你收到请求了吗?当您尝试获取值并解析它时,您能否调试应用程序以查看环境中存在哪些变量。

回答by Andrei

I only had the id atttribute set for the field and it didn't show up in the List items list. When I added the name attribute, it showed up.

我只为该字段设置了 id 属性,它没有显示在 List items 列表中。当我添加 name 属性时,它出现了。