How-to: File Uploading Using JSP/Servlet

Cansin

As a surprise, I saw that JSPs are not capable of handling multipart form data. So, I have spent some time on figuring out how to upload a file to the server using JSP/Servlet.

There are two known libraries to use to be able to handle multipart form data; Apache Commons’ FileUpload and OReilly’s COS. I have found latter easy to use. At COS, there are two approaches to handle multipart request; push and pull model. You can either use MultipartRequest class  to handle the request (push model) just like you do at a regular request (i.e. using req.getParameter()); or use MultipartParser class to do the job by iterating among request objects (pull model).

You can download the .war file which contains source codes and classes mentioned below at here.

For today’s example, I will use MultipartRequest class to do the uploading, since it has the same logic as HttpServletRequest class. But first we need to include COS’s jar to our war project. You can do it easily at Netbeans by right-clicking at Libraries at Projects view under your project and hitting Add JAR/Folder…. That’s it, you’re ready to go now.

If you want to be able to see javadocs of COS, you should hit Add Library… and Create… a new Class Library, then you can give the path of COS’s jar, javadoc and source. A little bit trickier than adding just a jar, but really worth it.

To be able to upload a file, we need to have a JSP to render the upload form, and a Servlet to handle incoming requests. Lets first start with our servlet. At this servlet, Upload, we’re receiving incoming request at processRequest(req,res) method. At this method, incoming HttpServletRequest is cast to MultipartRequest. If casting can be done, it means form is submitted (i.e an actual multipart request has arrived) and we’re ready to handle our uploaded file. If casting throws an IOException, it means our form is not yet submitted (i.e user just tries to see upload form) and we should show upload form to the user.

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try {
        /**
         * Parse request using MultiparRequest since it is a multipart/form-data
         * throws IOException if request is not multipart
         *
         * So, if form is not yet submitted Upload servlet will show upload.jsp as response
         * if form is submitted Upload servlet will process the request at upload() function
         */
        MultipartRequest multipartRequest = new MultipartRequest(request, getServletContext().getRealPath("/tmp/"), /* 1MB */ 1024 * 1024, new DefaultFileRenamePolicy());
        if (multipartRequest.getParameter("save") != null) {
            upload(request, response, multipartRequest);
        } else {
            throw new IOException("Display Upload Dialogue");
        }
    } catch (IOException ex) {
        displayUpload(request, response);
    }
}

Showing the upload form is trivial, we’re just forwarding the Servlet’s response to upload.jsp file. The form element at upload.jsp is a minimal file upload form (i.e. you should always include method and enctype attributes as it is, and have at least one file typed input).

private void displayUpload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    /**
     * Display upload.jsp as response.
     * It is a good convention to use JSPs as Views (i.e. to show response)
     * and Servlets as Controllers (i.e. to catch request and process)
     */
    getServletContext().getRequestDispatcher("/WEB-INF/jspf/upload.jsp").forward(request, response);
}
<form action="Upload" enctype="multipart/form-data" method="post">
<input name="uploaded" type="file" />
<input name="save" type="submit" value="Upload" />
</form>

Now all we have to do is to handle incoming uploaded file. MultipartRequest, when first initialized, stores every uploaded file to a temporary location that you gave (In our case it’s under tmp/ folder). Than you can manipulate those temporary files by any way you want. In this example we’re just moving the uploaded file from tmp/ folder to data/ folder, and changing it’s name to uploaded while keeping the original file format.

private void upload(HttpServletRequest request, HttpServletResponse response, MultipartRequest multipartRequest) throws IOException {
 
    /**
     * Get uploaded file
     */
    File tmpFile = multipartRequest.getFile("uploaded");
 
    /**
     * Do whatever adjustment you want to this temporary file
     * .
     * .
     * .
     */
 
    /**
     *  Move temporary file to actual destination
     */
    File dirToMove=new File(getServletContext().getRealPath("/data"));
    String newFileName= "uploaded"+tmpFile.getName().substring(tmpFile.getName().lastIndexOf('.'));
    File fileToMove=new File(dirToMove,newFileName);
    tmpFile.renameTo(fileToMove);
 
    /**
     * Delete temporary file
     */
    tmpFile.delete();
 
    /**
     * Display newly uploaded file
     */
    response.sendRedirect("data/"+newFileName);
}

I think I have covered some basics of file uploading. If you need more information (especially about pulling method which I didn’t cover at all), I recommend you to look at O’Reily’s COS’s javadocs at here.

Have a good day.


3 Responses to “How-to: File Uploading Using JSP/Servlet”