Handling file upload in Servlet 3.0

Ability to handle file upload is one of the common requirements for web application. Before Servlet Specification 3.0 if you wanted to handle file upload you would have to use external library such as Apache Commons File upload to handle the file upload.

But starting from Servlet Specification 3.0 you dont need external library instead there is a native support. I wanted to try this so i followed these steps


  1. First i did create a index.jsp file like this

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <%@page
    language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <html>
    <head>
    <title>index</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    </head>
    <body>
    <form action="hellofileupload" method="post" enctype="multipart/form-data" >
    <table>
    <tr>
    <td>File</td>
    <td><input type="file" name="samplefile" /></td>
    </tr>
    <tr><td><input type="submit"></td></tr>
    </table>
    </form>

    </body>
    </html>


    If you want to upload a file to the server you will have to set form encoding type to multipart/form-data. The index.jsp will submit form to the hellofileupload url and thats where our HelloFileUploadAnnotationServlet will come in

  2. Next create a HelloFileUploadAnnotationServlet like this

    package com.webspherenotes.j2ee6.servlet;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Collection;
    import java.util.Iterator;

    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;

    /**
    * Servlet implementation class HelloFileUploadAnnotationServlet
    */
    @WebServlet("/hellofileupload")
    @MultipartConfig
    public class HelloFileUploadAnnotationServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    System.out.println("Entering HelloFileUploadAnnotationServlet.doPost()");
    response.setContentType("text/html");
    response.getWriter().println("Hello from FileUploadServlet");
    Collection fileParts = request.getParts();
    Iterator filePartIt = fileParts.iterator();
    while(filePartIt.hasNext()){
    Part filePart = filePartIt.next();
    System.out.println("File Name " + filePart.getName());
    System.out.println("File Size " + filePart.getSize());

    System.out.println("File Content ");
    BufferedReader fileReader =
    new BufferedReader(new InputStreamReader(filePart.getInputStream()));
    String line = null;
    while(( line = fileReader.readLine()) != null){
    System.out.println(line);
    }
    }

    System.out.println("Exiting HelloFileUploadAnnotationServlet.doPost()");
    }
    }

    In order to use the file upload first you will have to mark the Servlet using MultipartConfig annotation, indicating that instances of the Servlet expect requests that conform to the multipart/form-data MIME type.
    Then inside the doPost() method you can use the method of HttpServletRequest to get access to the uploaded file.

1 comment: