Thursday, November 10, 2005

upload file using Apache commons fileupload

I write servlet to process upload file recently, I found the apache commons fileupload is a suitable package for this kind of programming, following is the help message found in package.

Package org.apache.commons.fileupload Description
Component for handling html file uploads as given by rfc 1867 RFC 1867.
Normal usage of the package involves DiskFileUpload parsing the HttpServletRequest and returning a list of FileItem's. These FileItem's provide easy access to the data given in the upload. There is also a low level api for manipulating the upload data encapsulated in the MultipartStream class.
Normal usage example: public void doPost(HttpServletRequest req, HttpServletResponse res)
{
DiskFileUpload fu = new DiskFileUpload();
// maximum size before a FileUploadException will be thrown
fu.setSizeMax(1000000);
// maximum size that will be stored in memory
fu.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
fu.setRepositoryPath("/tmp");
List fileItems = fu.parseRequest(req);
// assume we know there are two files. The first file is a small
// text file, the second is unknown and is written to a file on
// the server
Iterator i = fileItems.iterator();
String comment = ((FileItem)i.next()).getString();
FileItem fi = (FileItem)i.next();
// filename on the client
String fileName = fi.getName();
// save comment and filename to database
...
// write the file
fi.write("/www/uploads/" + fileName);
}
In the example above the first file is loaded into memory as a String. Before calling the getString method, the data may have been in memory or on disk depending on its size. The second file we assume it will be large and therefore never explicitly load it into memory, though if it is less than 4096 bytes it will be in memory before it is written to its final location. When writing to the final location, if the data is larger than the threshold, an attempt is made to rename the temporary file to the given location. If it cannot be renamed, it is streamed to the new location.