First download Apache Jakarta Commons FileUpload library from this link and add into classpath of your Application.
Validate HTTP Request
Now that you have installed the FileUpload library, you can start writing the code. First, we have to make sure the HTTP request is encoded in multipart format. This can be done using the static method isMultipartContent() of the ServletFileUpload class of the org.apache.commons.fileupload.servlet package:
// Parse the HTTP request…
}
In the above Java code snippet, request is a javax.servlet.http.HttpServletRequest object that encapsulates the HTTP request.
Parsing Form Data
Second, we will parse the form data contained in the HTTP request. Parsing the form data is very straightforward with the Apache Jakarta Commons FileUpload library:
List fileItemsList = servletFileUpload.parseRequest(request);
(In the above Java code snippet, DiskFileItemFactory is a class contained in the org.apache.commons.fileupload.disk package and List is an interface contained in the java.util package.)
If everything works fine, fileItemsList will contain a list of file items that are instances of FileItem of the org.apache.commons.fileupload package. A file item may contain an uploaded file or a simple name-value pair of a form field.
By default, the ServletFileUpload instance created by the above Java code uses the following values when parsing the HTTP request:
- Size threshold = 10,240 bytes. If the size of a file item is smaller than the size threshold, it will be stored in the memory. Otherwise it will be stored in a temporary file on disk.
- Maximum HTTP request body size = -1, which means the server will accept HTTP request bodies of any size.
- Repository = System default temp directory, whose value can be found by the Java code System.getProperty(“java.io.tmpdir”). Temporary files will be stored there.
If you do not like the default settings, you can change them using the methods setSizeThreshold() and setRespository() of the DiskFileItemFactory class and the setSizeMax() method of the ServletFileUpload class, like this:
diskFileItemFactory.setSizeThreshold(40960); /* the unit is bytes */
File repositoryPath = new File(“/temp”);
diskFileItemFactory.setRepository(repositoryPath);
ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
servletFileUpload.setSizeMax(81920); /* the unit is bytes */
If the size of the HTTP request body exceeds the maximum you set, the SizeLimitExceededException exception (fully qualified name: org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException) will be thrown when you call the parseRequest() method:
List fileItemsList = servletFileUpload.parseRequest(request);
/* Process file items… */
}
catch (SizeLimitExceededException ex) {
/* The size of the HTTP request body exceeds the limit */
}
Share or Bookmark This Post With :
org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn’t contain a multipart/form-data or multipart/mixed stream, content type header is null
If you are iterating the items from the multipart request without checking if an http request is encoded in multipart format
then you will get this error.
If ServletFileUpload.isMultipartContent(request) returns true then you need to get the fields via the upload.parseRequest(request) method.
eg:
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart)
{
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(1000000);
factory.setRepository( ConfigServlet.getTempDirectory() );
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(10000000);
// Parse the request
List
try {
items = upload.parseRequest(request);
for(FileItem item : items)
{
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
query.put(name, value);
}
else // file object
{
// do stuff for file request
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
query = new TreeMap
Map tbl = request.getParameterMap();
query.putAll( tbl );
}
Share or Bookmark This Post With :
class.upload.php -
This PHP script uploads files and manipulates images very easily. The perfect script to generate thumbnails or create a photo gallery! It can convert, resize and work on uploaded images in many ways, add labels, watermarks and reflections and other image editing features. You can use it for files uploaded through an HTML form, a Flash uploader, or on local files. It uses the GD library.

Share or Bookmark This Post With :
- dotProject – the Open Source Project Managem...
- The jQuery Form Plugin
- DocVerse has officially been acquired by Google
- HDGraph – a free tool for Windows to draw mu...
- RandomClass jQuery Plugin
- Paparazzi – a small utility for Mac OS X to ...
- Blocking SPAM using Postfix header_checks and Spam...
- Facebook, Paypal team up for virtual goods payment
- Opera Mini – the next generation mobile brow...
- XML/SWF Charts – powerful tool to create att...
- Send emails using PHPMailer an...
- Firefox Addons Essential for S...
- Free SEO Tools From SEOMoz.org...
- URL Rewriting for PHP Web Appl...
- Multiple Instances of Tomcat w...
- Apache 2.x + Tomcat 4.x + Load...
- Pagination in JSP/Java
- PHP – Mysql Open Source ...
- java.lang.OutOfMemoryError: Ja...
- HOW TO Subversion+Apache on Fe...





