[Demo] Multipart File Upload in Servlet 3.0 Using Multipart Config

Java EE introduced built in support for handling multipart MIME file uploads in Servlet 3.0 (Java EE 6). If you enable this functionality on a servlet, the container will make additional methods available on an HttpServletRequest to get all parts available on a request and also to get one of those parts passing in its name. We will see a demo of the multipart file upload in Servlet 3.0 using multipart config.

For theory and explanations on APIs, please refer to http://javajee.com/multipart-file-upload-in-servlet-30-using-multipart-config.

You may want to package the application as a war, deploy it into tomcat and then start tomcat manually following steps mentioned @ http://javajee.com/packaging-and-deploying-a-web-application-into-standalone-apache-tomcat as you may find issues in finding the real path to save file when directly working from eclipse integrated with tomcat.

 

HTML Form

We will need an html form to upload the file from client side. To send the request as multipart/form-data, the enctype attribute must be supplied and set to multipart/form-data, so that no characters are encoded. Standard way of writing file to a disk is using a FileOutputStream. However the new Part interface also provides a method to write. So we will also have a radio button asking client on which method to use: Part.write or using FileOutputStream.

<!DOCTYPE html>

<html lang="en">

<head>

<title>File Upload</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body>

                <form method="POST" action="upload" enctype="multipart/form-data">

                   File:

                   <input type="file" name="file" id="file" />

                   <br />

                   <br />

                   <input type="submit" value="Upload" name="upload" id="upload" />

                   <br/>

                   </br>

                   </br>

                   Write Options:

                   <br/> <input type="radio" name="writemethod" value="partwrite">Using Part.write()</input>

                    <br/> <input type="radio" name="writemethod" value="fileoutputstream">Creating FileOutputStream</input>

                </form>

</body>

</html>

 

The Servlet

You need a servlet that has the url pattern “upload”. Out servlet will ask a method processRequest to process this request passing the request and response object. This method will first retrieve the part for the file parameter. We will then calculate the filename from the part. Based on the radio button value for write options, we will decide whether to write using Part’s write method or using the standard way of FileOutputStream. We will also capture the time elapsed for writing.

 

FileUploadServlet.java

package com.javajee.servlets;

 

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.PrintWriter;

 

import javax.servlet.ServletException;

import javax.servlet.annotation.MultipartConfig;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.Part;

 

import org.apache.catalina.connector.Request;

 

@WebServlet(name = "FileUploadServlet", urlPatterns = { "/upload" })

@MultipartConfig

public class FileUploadServlet extends HttpServlet {

 

  private static final long serialVersionUID = 1L;

  private static final String uploadFolderName = "uploads";

  private static volatile String uploadFolder = null;

 

  public FileUploadServlet() {

    super();

  }

 

  protected void doPost(HttpServletRequest request, HttpServletResponse response)

      throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");

    final PrintWriter out = response.getWriter();

    try {

      processRequest(request, response);

    } catch (IOException e) {

      out.print("<br>ERROR! " + e.getMessage());

    }

 

  }

 

  protected void processRequest(HttpServletRequest request,

      HttpServletResponse response) throws ServletException, IOException {

 

    final PrintWriter out = response.getWriter();

 

    final Part filePart = request.getPart("file");

    final String fileName = getFileName(filePart);

    final String serverFilePath = getUploadFolder() + File.separator + fileName;

 

    long timeBefore = System.currentTimeMillis();

 

    String writeMethod = request.getParameter("writemethod");

    if (writeMethod.equals("partwrite")) {

      out.println("<br>Using Part.write method...");

      filePart.write(serverFilePath);

    } else {

      out.println("<br>Using Part.write method...");

      writeToFileUsingFileOutputStream(filePart.getInputStream(),

          serverFilePath);

    }

 

    long timeAfter = System.currentTimeMillis();

 

    out.println("<br>New file " + fileName + " created at " + serverFilePath);

    out.println("<br>Time elapsed= " + (timeAfter - timeBefore));

  }

 

  private String getUploadFolder() { // synchronization

                                     // not handled.

    if (uploadFolder == null) {

      String contextRealPath = getServletContext().getRealPath("/");

      uploadFolder = contextRealPath + File.separator + uploadFolderName;

      File dir = new File(uploadFolder);

 

      if (!dir.exists()) {

        boolean create = dir.mkdir();

        if (create) {

          System.out.println("Uploads directory created:" + uploadFolder);

        } else {

          throw new RuntimeException("Directory Cannot Be Created!");

        }

      }

    }

    return uploadFolder;

  }

 

  private void writeToFileUsingFileOutputStream(InputStream filecontent,

      String filePath) throws IOException {

    OutputStream out = null;

    try {

      out = new FileOutputStream(new File(filePath));

 

      int read = 0;

      final byte[] bytes = new byte[1024];

 

      while ((read = filecontent.read(bytes)) != -1) {

        out.write(bytes, 0, read);

      }

    } finally {

      if (out != null) {

        out.close();

      }

      if (filecontent != null) {

        filecontent.close();

      }

    }

 

  }

 

  private String getFileName(final Part part) {

    final String partHeader = part.getHeader("content-disposition");

    String[] sections = partHeader.split(";");

    for (String content : sections) {

      if (content.trim().startsWith("filename")) {

        return content.substring(content.indexOf('=') + 1).trim()

            .replace("\"", "");

      }

    }

    return null;

  }

}

You may play around with the attributes of @MultipartConfig. You may also use the mutipart-config element in the web.xml instead of the annotation.

 

More about getFileName method

A look at the request sent from client through a tool such as tcpmon or wireshark will give you a better idea. I have captured two screenshots from my tcpmon leaving some of the uploaded file contents from middle.

As you can see from the images, there is a part for all elements in the form. Each part starts with a boundary delimiter (e.g. ------WebKitFormBoundarysAGlmoydAOw5Bfh1). Every part also has a Content-Disposition header which contains information such as name of field, filename of the original file in case of files etc. So we have to get the part for file, get the Content-Disposition header for that part, split the header value using semicolon (;) into sections, iterate through the sections until you see a section starting with filename and then retrieve the filename.

 

Execution

You may want to package the application as a war, deploy it into tomcat and then start tomcat manually following steps mentioned @ http://javajee.com/packaging-and-deploying-a-web-application-into-standalone-apache-tomcat as you may find issues in finding the real path to save file when directly working from eclipse integrated with tomcat.

You should first run your html form (e.g. http://localhost:8080/Form-Processing/UploadForm.html where Form-Processing is my context folder in tomcat, UploadForm.html is the html form page which is created directly under the WebContent folder).

 

Select a file along with a write option, and upload it.

If upload is successful you will get the success page with information about the option you selected and time elapsed.

 

Development and Execution Environment

Eclipse Luna Java EE IDE for Web Developers and Apache Tomcat 8.0.18, using Servlet spec 3.1.

 

References

http://javajee.com/multipart-file-upload-in-servlet-30-using-multipart-config

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)