Uploading File to server using Servlet and JSP is a common task in Java web application. Before coding your Servlet or JSP to handle file upload request, you need to know little bit about File upload support in HTML and HTTP protocol. If you want your user to choose files from file system and upload to server than you need to use <input type="file"/>. This will enable to choose any file form file system and upload to server. Next thing is that form method should be HTTP POST with enctype as multipart/form-data, which makes file data available in parts inside request body. Now in order to read those file parts and create a File inside Servlet can be done by using ServletOutputStream. It's better to use Apache commons FileUpload, an open source library. Apache FileUpload handles all low details of parsing HTTP request which confirm to RFC 1867 or "Form based File upload in HTML”, when you set form method post and content type as "multipart/form-data".
1) DiskFileItemFactory is default Factory class for FileItem. When Apache commons read multipart content and generates FileItem, this implementation keep file content either in memory or in disk as temporary file, depending upon threshold size. By default DiskFileItemFactory has threshold size of 10KB and generates temporary files in temp directory, returned by System.getProperty("java.io.tmpdir"). Both of these values are configurable and it's best to configure these for production usage. You may get permission issues if user account used for running Server doesn't have sufficient permission to write files into temp directory.
2) Choose threshold size carefully based upon memory usage, keeping large content in memory may result in java.lang.OutOfMemory, while having too small values may result in lot's of temporary files.
3) Apache commons file upload also provides FileCleaningTracker to delete temporary files created by DiskFileItemFactory. FileCleaningTracker deletes temporary files as soon as corresponding File instance is garbage collected. It accomplish this by a cleaner thread which is created when FileCleaner is loaded. If you use this feature, than remember to terminate this Thread when your web application ends.
4) Keep configurable details e.g. upload directory, maximum file size, threshold size etc in config files and use reasonable default values in case they are not configured.
5) It's good to validate size, type and other details of Files based upon your project requirement e.g. you may wants to allow upload only images of certain size and certain types e.g. JPEG, PNG etc.
File Upload Example in Java Servlet and JSP
Here is the complete code for uploading files in Java web application using Servlet and JSP. This File Upload Example needs four files :
1. index.jsp which contains HTML content to setup a form, which allows user to select and upload file to server.
2. FileUploader Servlet which handles file upload request and uses Apache FileUpload library to parse multipart form data
3. web.xml to configure servlet and JSP in Java web application.
4. result.jsp for showing result of file upload operation.
In summary just keep three things in mind while uploading files using Java web application
1) Use HTML form input type as File to browse files to upload
2) Use form method as post and enctype as multipart/form-data
3) Use Apache commons FileUpload in Servlet to handle HTTP request with multipart data.
In order to compile and run this Java web application in any web server e.g. Tomcat, you need to include following dependency JAR in WEB-INF lib folder.
If you are using Maven than you can also use following dependencies :
That's all on How to upload Files using Servlet and JSP in Java web application. This File Upload example can be written using JSP, Filter or Servlet because all three are request’s entry point in Java web application. I have used Servlet for handling File upload request for simplicity. By the way from Servlet 3.0 API, Servlet is supporting multipart form data and you can use getPart() method of HttpServletRequest to handle file upload.
Related Java Web Tutorials from Javarevisited Blog