Upload.html
1 2 3 4 5 6 7 8 9 10 |
<html> <head></head> <body> <p>Commons File Upload Example</p> <form action="Commonsfileuploadservlet" enctype="multipart/form-data" method="POST"> <input type="file" name="file1"><br> <input type="Submit" value="Upload File"><br> </form> </body> </html> |
CommonsFileUploadServlet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import java.util.List; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class CommonsFileUploadServlet extends HttpServlet { private static final String TMP_DIR_PATH = "c:\\tmp"; private File tmpDir; private static final String DESTINATION_DIR_PATH = "/files"; private File destinationDir; public void init(ServletConfig config) throws ServletException { super.init(config); tmpDir = new File(TMP_DIR_PATH); if (!tmpDir.isDirectory()) { throw new ServletException(TMP_DIR_PATH + " is not a directory"); } String realPath = getServletContext().getRealPath(DESTINATION_DIR_PATH); destinationDir = new File(realPath); if (!destinationDir.isDirectory()) { throw new ServletException(DESTINATION_DIR_PATH + " is not a directory"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); response.setContentType("text/plain"); out.println("<h1>Servlet File Upload Example using Commons File Upload</h1>"); out.println(); DiskFileItemFactory fileItemFactory = new DiskFileItemFactory(); // Set the size threshold, above which content will be stored on disk. fileItemFactory.setSizeThreshold(1 * 1024 * 1024); //1 MB // Set the temporary directory to store the uploaded files of size above threshold. fileItemFactory.setRepository(tmpDir); ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory); try { // Parse the request List items = uploadHandler.parseRequest(request); Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); // Handle Form Fields. if (item.isFormField()) { out.println("File Name = " + item.getFieldName() + ", Value = " + item.getString()); } else { //Handle Uploaded files. out.println("Field Name = " + item.getFieldName() + ", File Name = " + item.getName() + ", Content type = " + item.getContentType() + ", File Size = " + item.getSize()); // Write file to the ultimate location. File file = new File(destinationDir, item.getName()); item.write(file); } out.close(); } } catch (FileUploadException ex) { log("Error encountered while parsing the request", ex); } catch (Exception ex) { log("Error encountered while uploading file", ex); } } } |
Web.xml
1 2 3 4 5 6 7 8 9 10 11 |
<servlet> <servlet-name>CommonsFileUploadServlet</servlet-name> <servlet-class>CommonsFileUploadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CommonsFileUploadServlet</servlet-name> <url-pattern>/Commonsfileuploadservlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/upload.html</welcome-file> </welcome-file-list> |