I have the following html form...
我有以下html表单...
<html>
<head><title>Upload Servlet</title></head>
<body><h2>Upload Servlet</h2>
<form name='uploadparams' enctype='multipart/form-data' action='' method='post'>
<label>Migrate Options From:
<select name='migrateFrom'>
<option></option>
<option value='version 1'>version 1</option>
</select>
</label>
<br/>
<input type='file' name='zipFile'>
<br/>
<input type='hidden' value='willnotshowupinservlet'/>
<button type='submit'>Submit</button>
</form>
</body>
</html>
The problem is that while I can read the file with a http parameter name of "zipFile" just fine my servlet does not see the other parameters "willnotshowupinservlet" and "migrateFrom". Are file upload forms only able to have one input (the file input)?
问题是,虽然我可以读取http参数名称为“zipFile”的文件,但我的servlet没有看到其他参数“willnotshowupinservlet”和“migrateFrom”。文件上传表单只能有一个输入(文件输入)吗?
1 个解决方案
#1
5
They are indeed not available as regular request parameters because you've set the form encoding to multipart/form-data
(which is indeed mandatory in order to be able to include file content in the request body). You have to parse the request body conform the multipart/form-data
specification. The getParameter()
calls of Servlet API only supports a form encoding of application/x-www-form-urlencoded
which is the default enctype
of a HTML <form>
element.
它们确实不能作为常规请求参数使用,因为您已将表单编码设置为multipart / form-data(为了能够在请求正文中包含文件内容,这确实是必需的)。您必须解析请求正文符合multipart / form-data规范。 Servlet API的getParameter()调用仅支持application / x-www-form-urlencoded的表单编码,这是HTML
A commonly used API to ease the job is Apache Commons FileUpload. Or, when you're already on Servlet 3.0, you need to annotate the servlet with @MultipartConfig
. You can find concrete examples of both approaches in this answer.
Apache Commons FileUpload是一个常用的API来简化工作。或者,当您已经使用Servlet 3.0时,需要使用@MultipartConfig注释servlet。您可以在此答案中找到两种方法的具体示例。
#1
5
They are indeed not available as regular request parameters because you've set the form encoding to multipart/form-data
(which is indeed mandatory in order to be able to include file content in the request body). You have to parse the request body conform the multipart/form-data
specification. The getParameter()
calls of Servlet API only supports a form encoding of application/x-www-form-urlencoded
which is the default enctype
of a HTML <form>
element.
它们确实不能作为常规请求参数使用,因为您已将表单编码设置为multipart / form-data(为了能够在请求正文中包含文件内容,这确实是必需的)。您必须解析请求正文符合multipart / form-data规范。 Servlet API的getParameter()调用仅支持application / x-www-form-urlencoded的表单编码,这是HTML
A commonly used API to ease the job is Apache Commons FileUpload. Or, when you're already on Servlet 3.0, you need to annotate the servlet with @MultipartConfig
. You can find concrete examples of both approaches in this answer.
Apache Commons FileUpload是一个常用的API来简化工作。或者,当您已经使用Servlet 3.0时,需要使用@MultipartConfig注释servlet。您可以在此答案中找到两种方法的具体示例。