// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) return ;
// Apache commons upload section
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List items = new ArrayList() ;
try {
items = upload.parseRequest(request);
} catch (FileUploadException fue) {
LOGGER.error("Error uploading file: " + fue.toString());
}
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
// **************************************************
// Process your file input here! *****
// **************************************************
} else {
String name = item.getFieldName();
String value = item.getString();
// **************************************************
// Process your name and value pairs here! *****
// **************************************************
LOGGER.debug("Found field " + name + " and value " + value) ;
}
}
Notes from my day to day Java and J2EE software development experiences and other related software development technologies.
Friday, August 10, 2007
Handling multi-part form data without pain
Apache commons uploader has some nice built-in frameworks to handle multi-part form data. In this example part of the form data are file uploads and other parts are form fields. As you will see Apache commons uploader makes this task extremely easy...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment