0

I'm creating a JSP/Servlet web application and I'd like to upload a file to a Servlet with progress bar. This is an example of uploading file with jsp and servlet. Its working but i want to add to it a progress bar.

The FileUploadHandler.java

 import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * Servlet to handle File upload request from Client
 * @author Javin Paul
 */
public class FileUploadHandler extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private final String UPLOAD_DIRECTORY = "uploads";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String uploadPath = getServletContext().getRealPath("")
                + File.separator + UPLOAD_DIRECTORY;

        String name = null;
        //process only if its multipart content
        if(ServletFileUpload.isMultipartContent(request)){
            try {
                List<FileItem> multiparts = new ServletFileUpload(
                                         new DiskFileItemFactory()).parseRequest(request);

                for(FileItem item : multiparts){
                    if(!item.isFormField()){
                         name = new File(item.getName()).getName();
                        item.write( new File(uploadPath+name));

                    }
                }

               //File uploaded successfully
               request.setAttribute("message", "File Uploaded Successfully "+uploadPath+ name);
            } catch (Exception ex) {
               request.setAttribute("message", "File Upload Failed due to " + ex);
            }          

        }else{
            request.setAttribute("message",
                                 "Sorry this Servlet only handles file upload request");
        }

        request.getRequestDispatcher("/result.jsp").forward(request, response);

    }

}

The upload.jsp

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>File Upload Example in JSP and Servlet - Java web application</title>
    </head>

    <body> 
        <div>
            <h3> Choose File to Upload in Server </h3>
            <form action="upload" method="post" enctype="multipart/form-data">
                <input type="file" name="file" />
                <input type="submit" value="upload" />
            </form>          
        </div>

    </body>
</html>

The result.jsp

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>File Upload Example in JSP and Servlet - Java web application</title>
    </head>

    <body> 
        <div id="result">
            <h3>${requestScope["message"]}</h3>
        </div>

    </body>
</html>

The web.xml

 <servlet>
    <servlet-name>FileUploadHandler</servlet-name>
    <servlet-class>FileUploadHandler</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>FileUploadHandler</servlet-name>
    <url-pattern>/upload</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  • 1
    Possible duplicate of [Upload progress bar Java Servlet?](http://stackoverflow.com/questions/4192375/upload-progress-bar-java-servlet) – worpet Mar 06 '17 at 15:34

1 Answers1

-1

Though late, try using following plugin, which makes your UI beautiful and super easy.

https://github.com/blueimp/jQuery-File-Upload

Java examples are as follows:

https://github.com/blueimp/jQuery-File-Upload/wiki

tarunkumar
  • 861
  • 2
  • 15
  • 30