Environment:
Tomcat5.6
Commmons-fileupload-1.3.jar
Commmons-io-2.4.jar
JSP
Code: UTF-8
Temporary Folder: fileupload/tmp is relative to the root directory of the website.
Storage location of the uploaded file: fileupload
Traditional API upload Method
// Fileload01.htm
Copy codeThe Code is as follows:
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Html>
<Body>
<Form method = "POST" enctype = "multipart/form-data" action = "traditionalapi. jsp">
File to upload: <input type = "file" name = "file" size = "40"> <br/>
<Input type = "submit" value = "Press"> to upload the file!
</Form>
</Body>
</Html>
// Traditionalapi. jsp
Copy codeThe Code is as follows:
<% @ Page contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" language = "java" %>
<% @ Page import = "java. io. File" %>
<% @ Page import = "java. util. List" %>
<% @ Page import = "org. apache. commons. fileupload. *" %>
<% @ Page import = "org. apache. commons. fileupload. disk. DiskFileItemFactory" %>
<% @ Page import = "org. apache. commons. fileupload. servlet. ServletFileUpload" %>
<%
Request. setCharacterEncoding ("UTF-8 ");
// File less than 10kb will be store in memory, otherwise in file system.
Final int threshold = 10240;
Final File tmpDir = new File (getServletContext (). getRealPath ("/") + "fileupload" + File. separator + "tmp ");
Final int maxRequestSize = 1024*1024*4; // 4 MB
// Check that we have a file upload request
If (ServletFileUpload. isMultipartContent (request ))
{
// Create a factory for disk-based file items.
FileItemFactory factory = new DiskFileItemFactory (threshold, tmpDir );
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload (factory );
// Set overall request size constraint.
Upload. setSizeMax (maxRequestSize );
List <FileItem> items = upload. parseRequest (request); // FileUploadException
For (FileItem item: items)
{
If (item. isFormField () // regular form field
{
String name = item. getFieldName ();
String value = item. getString ();
%>
<H1> <% = name %> --> <% = value %> <%
}
Else
{// File upload
String fieldName = item. getFieldName ();
String fileName = item. getName ();
File uploadedFile = new File (getServletContext (). getRealPath ("/") +
"Fileupload" + File. separator + fieldName + "_" + fileName );
Item. write (uploadedFile );
%>
<H1> upload file <% = uploadedFile. getName () %> done! </H1>
<%
}
}
}
%>
Streaming API upload Method
// Fileupload02.htm
Copy codeThe Code is as follows:
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Html>
<Body>
<Form method = "POST" enctype = "multipart/form-data" action = "streamingapi. jsp">
File to upload: <input type = "file" name = "file" size = "40"> <br/>
<Input type = "submit" value = "Press"> to upload the file!
</Form>
</Body>
</Html>
// Streamingapi. jsp
Copy codeThe Code is as follows:
<% @ Page contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" language = "java" %>
<% @ Page import = "java. io. *" %>
<% @ Page import = "java. util. List" %>
<% @ Page import = "org. apache. commons. fileupload. *" %>
<% @ Page import = "org. apache. commons. fileupload. util. Streams" %>
<% @ Page import = "org. apache. commons. fileupload. servlet. ServletFileUpload" %>
<%
Request. setCharacterEncoding ("UTF-8 ");
// Check that we have a file upload request
If (ServletFileUpload. isMultipartContent (request ))
{
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload ();
// Parse the request
FileItemIterator iter = upload. getItemIterator (request );
While (iter. hasNext ())
{
FileItemStream item = iter. next ();
String fieldName = item. getFieldName ();
InputStream is = item. openStream ();
If (item. isFormField () // regular form field
{
%>
<! -- Read a FileItemStream's content into a string. -->
<H1> <% = fieldName %> --> <% = Streams. asString (is) %> <%
}
Else
{// File upload
String fileName = item. getName ();
File uploadedFile = new File (getServletContext (). getRealPath ("/") +
"Fileupload" + File. separator + fieldName + "_" + fileName );
OutputStream OS = new FileOutputStream (uploadedFile );
// Write file to disk and close outputstream.
Streams. copy (is, OS, true );
%>
<H1> upload file <% = uploadedFile. getName () %> done! </H1>
<%
}
}
}
%>
Traditional API vs Streaming API
Streaming API upload speed is relatively fast. It saves the overhead of writing files to temporary files by using the memory.
Refer:
Http://stackoverflow.com/questions/11620432/apache-commons-fileupload-streaming-api
This page describes the traditional API of the commons fileupload library. The traditional API is a convenient approach. However, for ultimate performance, you might prefer the faster Streaming API.
Http://commons.apache.org/proper/commons-fileupload/using.html