简体   繁体   English

使用enctype =“multipart / form-data”的表单会导致访问隐藏字段的问题

[英]Does form with enctype=“multipart/form-data” cause problems accessing a hidden field

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.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.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"? 有关enctype =“multipart / form-data”的表单有什么不同吗? Or can you see some other error. 或者你能看到其他一些错误吗?

The servlet parses the parameters by default using application/x-www-form-urlencoded encoding. servlet默认使用application/x-www-form-urlencoded编码解析参数。 The multipart/form-data encoding however isn't supported in servlets until Servlet 3.0 . 但是,在Servlet 3.0之前,servlet中不支持multipart/form-data编码。 The getParameter() calls will all return null . getParameter()调用都将返回null

In Servlet 3.0, you should have used HttpServletRequest#getParts() instead to get all parts of a multipart/form-data request, including normal form fields. 在Servlet 3.0中,您应该使用HttpServletRequest#getParts()来获取multipart/form-data请求的所有部分,包括普通表单字段。 Prior to Servlet 3.0, you should have used Apache Commons FileUpload to parse multipart/form-data requests. 在Servlet 3.0之前,您应该使用Apache Commons FileUpload来解析multipart/form-data请求。 See also the following answer for a detailed example of both approaches: How to upload files to server using JSP/Servlet? 有关这两种方法的详细示例,请参阅以下答案: 如何使用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> . 请注意,如果您根本没有使用任何<input type="file">字段,那么您可以将编码从<form>移开。 It will then default to application/x-www-form-urlencoded . 然后默认为application/x-www-form-urlencoded

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")调用工作。

Indeed there is something different. 确实有一些不同的东西。

request.getParameter will only work for hardcoded URL parameters specified in action attribute of <form> element. request.getParameter仅适用于<form>元素的action属性中指定的硬编码URL参数。 In your case it does not contain any. 在你的情况下,它不包含任何。

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 . 我一直在使用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. 其中一个将是你隐藏的领域。

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代码吗?

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. 不确定这是否有帮助,但是我在jsp页面中使用了多部分表单,这些表单被提交到struts servlet,这些页面有我的Struts Action类中接收的隐藏字段(包含在Struts ActionForm中),所以我不认为在这里有任何困难。

Have you tried receiving this value as String and seeing what actually comes there? 您是否尝试过将此值作为字符串接收并查看实际存在的内容?

You'd check the servlet code itself. 您将检查servlet代码本身。 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. 您可以调试应用程序,以便在尝试获取值并解析它时查看环境中存在哪些变量。

I only had the id atttribute set for the field and it didn't show up in the List items list. 我只为该字段设置了id atttribute,它没有出现在List项目列表中。 When I added the name attribute, it showed up. 当我添加name属性时,它出现了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 enctype multipart/form-data 在多数据类型表单中的使用 - Usage of enctype multipart/form-data in multi data type form h:form enctype =“ multipart / form-data”不执行任何操作 - h:form enctype=“multipart/form-data” not firing any action enctype =“ multipart / form-data”破坏了我的应用 - enctype=“multipart/form-data” destroyed my app 在 enctype=&quot;multipart/form-data&quot; 请求不起作用之后 - after enctype="multipart/form-data" request not working 使用enctype =“ multipart / form-data”将选定的图像获取到控制器类吗? - Get selected image to Controller Class with enctype=“multipart/form-data”? 了解Spring MVC中的enctype = multipart / form-data的工作 - Understanding working of enctype = multipart/form-data in spring mvc 当提交的表单具有属性 enctype=&quot;multipart/form-data&quot; 时,如何在控制器中获取表单数据? - How to get form data in controller when the form that was submitted has the attribute enctype="multipart/form-data"? 使用 enctype=multipart/form-data 上传文件时出错,错误是上传的文件不是 multipart - error while file uploading using enctype=multipart/form-data , the error is the file bring uploaded is not multipart 尽管我写了“enctype=&quot;multipart/form-data”,但发生错误“当前请求不是多部分请求” - Although I wrote 'enctype="multipart/form-data', ERROR 'Current request is not a multipart request' happen 当表单具有enctype =“ multipart / form-data时,无法在数据库中插入图像 - Not able to Insert image in database when form has enctype="multipart/form-data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM