我创建了一个隐藏的表单元素
<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(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-data"的表单有什么不同吗?或者你能看到一些其他错误。
最佳答案
servlet默认使用application/x-www-form-urlencoded
解析参数编码。 multipart/form-data
然而,在 Servlet 3.0 之前,servlet 不支持编码。 . getParameter()
调用将全部返回 null
.
在Servlet 3.0中,你应该使用 HttpServletRequest#getParts()
而不是获取 multipart/form-data
的所有部分请求,包括普通表单字段。在 Servlet 3.0 之前,你应该使用 Apache Commons FileUpload解析 multipart/form-data
要求。有关这两种方法的详细示例,另请参阅以下答案:How to upload files to server using JSP/Servlet?
请注意,如果您没有使用任何 <input type="file">
字段,那么您可以将编码远离 <form>
.然后它将默认为 application/x-www-form-urlencoded
.
关于java - 使用 enctype ="multipart/form-data"的表单是否会导致访问隐藏字段时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2827778/