Dr. Julian Timpner

Java Servlet & JSP Form-based File Upload

Surprisingly, Java Servlets and JSP do not have build-in support for handling form-based file uploads. However, there are several open source libraries available for this purpose, among which the Apache Commons FileUpload is one of the most stable. This post will demonstrate how to use this library to handle form-based file uploads using JSP technology.

Two files will be required for this to work: the Apache Commons FileUpload library, as well as the Apache Commons IO library.

In order to install them correctly, one needs to extract the downloaded archives and copy the commons-fileupload-<version>.jar and commons-io-<version>.jar to the WEB-INF/lib directory of the web app that is to be developed. If these libraries rather be available to all web applications on the server, the jar files should be copied to $CATALINA_HOME/shared/lib/, where $CATALINE_HOME is the root directory of the Tomcat installation.

The following assumes that two files have been set up: first, a simple HTML file with a form that allows the user to select the file to be uploaded. Note that enctype=”multipart/form-data” is essential here.

<form method="POST" action="action.jsp" enctype="multipart/form-data">
  <input type="file" name="file"/>
  <input type="submit"/>
</form>

Second, a file action.jsp that receives the HTML file’s HTTP request and processes it. To do so, it will need to import several packages:

< %@page import="org.apache.commons.fileupload.servlet.*"%>
< %@page import="org.apache.commons.fileupload.disk.*"%>
< %@page import="org.apache.commons.fileupload.*"%>
< %@page import="java.io.InputStream"%>

The next thing to do is to check if the HTTP request is correctly encoded in the multipart format.

if (ServletFileUpload.isMultipartContent(request)){
  // Process the request
}

The actual parsing of the request is done via the ServletFileUpload class and the DiskFileItemFactory class. Here, the uploaded file is stored as a byte array in the session.

ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());

try {
  @SuppressWarnings("unchecked")
  List< fileitem> fileItemsList = servletFileUpload.parseRequest(request);
  for (FileItem fileItem : fileItemsList) {
    if (fileItem.isFormField()) {
      /* The fileItem is not a file, but rather a name-value pair. */
      String name = fileItem.getFieldName();
      String value = fileItem.getString();
      // business logic here
    } else {
      /* The fileItem is an uploaded file. */
      byte[] fileData = fileItem.get();
      session.setAttribute("file", fileData);
    }
  }
} catch (Exception ex) {
  /* Possible exceptions include the file exceeding the upload size limit. */
}

This is only a very basic example, of course, and may not perform well with large files for example. Moreover, you might want to change the default settings for parsing the HTTP request, using the methods setSizeThreshold() and setRespository() of the DiskFileItemFactory class and the setSizeMax() method of the ServletFileUpload class.

Tagged on: