是否使用 enctype="multipart/form-data" 形成表单?导致访问隐藏字段时出现问题

发布于 2024-09-01 23:00:29 字数 908 浏览 2 评论 0 原文

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

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

,并且我试图在 servlet 中使用这一行获取值(就像我之前所做的那样):

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

但是我得到了这个(第 33 行是上面的行):

java.lang.NumberFormatException: null
java.lang.Integer.parseInt(来源未知) java.lang.Integer.parseInt(来源未知) web.objects.UploadImage.doPost(UploadImage.java:33) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

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

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

樱娆 2024-09-08 23:00:30

Servlet 默认使用 application/x-www-form-urlencoded 编码来解析参数。然而,servlet 中不支持 multipart/form-data 编码,直到 Servlet 3.0getParameter() 调用都将返回 null

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

请注意,如果您根本没有使用任何 字段,那么您可以只保留远离

的编码。然后它将默认为 application/x-www-form-urlencoded

The servlet parses the parameters by default using application/x-www-form-urlencoded encoding. The multipart/form-data encoding however isn't supported in servlets until Servlet 3.0. The getParameter() calls will all return 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. Prior to Servlet 3.0, you should have used Apache Commons FileUpload to parse multipart/form-data requests. See also the following answer for a detailed example of both approaches: How to upload files to server using 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.

悲歌长辞 2024-09-08 23:00:30

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

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

    //rest of the form here

</form>

这将使 request.getParameter("imgUploadObjId") 调用正常工作。

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

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

2024-09-08 23:00:30

确实有不一样的地方。

request.getParameter 仅适用于

元素的 action 属性中指定的硬编码 URL 参数。在你的情况下它不包含任何内容。

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

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

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

Indeed there is something different.

request.getParameter will only work for hardcoded URL parameters specified in action attribute of <form> element. 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.

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.

淡笑忘祈一世凡恋 2024-09-08 23:00:30

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

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

梦行七里 2024-09-08 23:00:30

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

您是否尝试过以字符串形式接收该值并查看实际情况?

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.

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

半山落雨半山空 2024-09-08 23:00:30

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

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.

傾旎 2024-09-08 23:00:30

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文