Reputation: 33571
I have the following html form...
<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)?
Upvotes: 1
Views: 10748
Reputation: 1108632
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.
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.
Upvotes: 5