Search code examples
javaservletsfile-upload

how to monitor progress of uploading file in servlet 3.1


i want to monitor the progress of the file which is getting uploaded i m using servlet 3.1

i know that by the specs of servlet 3.1 , i don't need apache common fileUpload i can do that only by the Part class

i did this

InputStream inputStream = null; 

       double s;
       String size=null;
       String contentType=null;
       String submittedName=null;
       String tim=null;
       String actualLocation=null;
       System.out.println("getting it also here");
       Part filePart = request.getPart("file");
       System.out.println("getting it here");
       if(description.equals(""))
       {
           System.out.println("please provide some description about file");
       }
       else{

            if (filePart != null )
              {
                    s=(double)filePart.getSize();

                    double MB=(s/1048576);
                   if(MB != 0)

                       {
                             size=String.valueOf((float)MB);

                             contentType=filePart.getContentType();

                             submittedName=getFormatted(filePart.getSubmittedFileName());

                             tim=String.valueOf(System.currentTimeMillis());

                             actualLocation=name+"-"+commun+"-"+tim+"-"+submittedName;
                              System.out.println("getting it there");
                             inputStream = filePart.getInputStream();
                              System.out.println("getting it here & there");

                                 try
                                 {
                                     File file=new File(x,actualLocation);
                                     Files.copy(inputStream, file.toPath());
                                  }

i know i can use a listener to monitor the progress , but the main question is that where should i set progress listener i thought it should be set with Part filePart.before System.out.println("getting it here")

this is a screenshot of my project

u can see that chrome is displaying the progress of uploading (in the bottom)(but i want a much more impressive progress bar)

when i ran my project i thought that in the console i'll see "getting it also here " while uploading but i found that control is not coming to that part unless uploading is completed.

i'm a bit confuse at which segment the uploading is happening and where should i set progress listener


Solution

  • You don't need to do anything in server side for upload progress bar. You can do that just using javascript, html and css.

    If you are using jQuery take a look at this pugin http://malsup.com/jquery/form/. Here is a file upload progress bar demo link http://malsup.com/jquery/form/progress.html. There are two more demos and plenty more examples in the website.

    And you can take a look at this tutorial http://codular.com/javascript-ajax-file-upload-with-progress for doing this with html5 and javascript. Just ignore the PHP part.

    You should not have to change anything in your java code, cause the servlet method will be hit after the file upload has been completed.