You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by DAVID TURNER <DT...@nhbb.com> on 2007/08/22 14:44:17 UTC
[FileUpload] Form parameters after a file input parameter not
beingprocessed
I have a simple form that contains three input fields, one hidden, one
checkbox, and one of type file. If I place the hidden and checkbox input
fields in front of the file input field then the servlet code picks up the
regular form fields. If one or both of these regular form fields are
placed after the file input field then FileUpload doesn't see them.
Can regular form fields be placed after a file input field? How can I get
this working so I can have other form fields after the file input field?
Versions being used:
commons fileupload - 1.2
commons io - 1.3.2
jdk 6
Sample HTML form (the hidden & checkbox wont get picked up unless placed
before the file input parm):
<form name="frm_upload" method="post"
action="WFImageUploadServlet" enctype="multipart/form-data">
<table>
<tr>
<td>Image File Name:</td>
<td>
<input type="file" id="imageFileName"
name="imageFileName" size="50" value="">
</td>
</tr>
</table>
<input type="hidden" id="imageId" name="imageId" value="" />
<input type="checkbox" id="imageOverwrite" name="imageOverwrite"
value="overwrite" />Overwrite Image on Server
<input type="submit" value="Upload">
</form>
Servlet code:
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException
{
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent
(request);
if (!isMultipart)
throw new ServletException("HTML Form is not a
multipart form");
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
// Sets the size threshold beyond which files are written
directly to disk.
factory.setSizeThreshold(2000);
// Sets the directory used to temporarily store files that
are larger than the configured size threshold
factory.setRepository(new File("c:\\temp"));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
// Sets the maximum allowed size of a complete request. -1
is no limit
upload.setSizeMax(-1);
// Sets the maximum allowed size of a single uploaded file
//upload.setFileSizeMax(fileSizeMax);
// Parse the request
List /* FileItem */ items = null;
try {
items = upload.parseRequest(request);
} catch(FileUploadException fue) {
throw new ServletException(fue.getMessage());
}
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) { // this is a regular
form field
String name = item.getFieldName();
String value = item.getString();
if (logger.isDebugEnabled()) {
logger.debug("Parameter name: " +
name);
logger.debug("Parameter value: " +
value);
}
if (name.equalsIgnoreCase("imageId"))
imageId = value;
if (name.equalsIgnoreCase("imageOverwrite"
))
imageOverwrite = true;
} else { // this is the file attachment
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType =
item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
if (logger.isDebugEnabled()) {
logger.debug("Parameter: " +
fieldName);
logger.debug("File name: " +
fileName);
logger.debug("Content type: " +
contentType);
logger.debug("Is in memory: " +
isInMemory);
logger.debug("Size in bytes: " +
sizeInBytes);
}
String imageDirectory = "c:\\temp\\";
File uploadedFile = new
File(imageDirectory + fileName);
// save the image file attachment to the
file system
try {
item.write(uploadedFile);
} catch (Exception e) {
logger.error("Error saving image
to file system", e);
throw new ServletException("Could
not save image to the file system");
}
}
}
}