用enctype =" multipart / form-data"导致访问隐藏字段时出现问题 [英] Does form with enctype="multipart/form-data" cause problems accessing a hidden field

查看:331
本文介绍了用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>

我试图用servlet中的这一行来获取值(正如我以前所做的那样) :

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"));

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

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


java.lang.NumberFormatException:null

java.lang.Integer.parseInt(Unknown Source)
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)

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)

对于enctype =multipart / form-数据?或者你可以看到其他的错误。

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

推荐答案

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

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.

在Servlet 3.0中,您应该使用 HttpServletRequest#getParts() 来代替 multipart / form-data 请求,包括标准表单域。在使用Servlet 3.0之前,您应该使用 Apache Commons FileUpload 来解析 multipart / form-data 请求。有关这两种方法的详细示例,另请参阅以下答案: How使用JSP / Servlet将文件上传到服务器?

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?

请注意,如果您没有使用任何< input type =文件> 字段,那么你可以把编码从< form> 中留下。它将默认为 application / x-www-form-urlencoded

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.

这篇关于用enctype =&quot; multipart / form-data&quot;导致访问隐藏字段时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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