Link to home
Start Free TrialLog in
Avatar of mock5c
mock5c

asked on

Progress Monitor for file upload progress

I would like to add a progress monitor for monitoring file upload progress.

Below is code that does the file upload.  I've also posted the doPost method from the servlet.

I've already added a monitor for other activities such as validating files.  If this were to be similar, I would think I would need to run the executeMethod() function in a SwingWorker thread.

How does the executeMethod() function, or any HttpClient method, return progress to a web start application's ProgressMonitor bar?

I've stumbled upon an example where a ProgressListener is implemented but this seems to be on server-side.  I'm not sure how the JNLP app would get updated with bytes uploaded in that case.

String targetURL = "http://localhost:8080/TestApp/upload
MultipartPostMethod filePost = new MultipartPostMethod(targetURL);

try {
   String zipFilePath = compressor.getCanonicalPath();
   filePost.addParameter(zipFilePath, targetFile);
   HttpClient client = new HttpClient();
   client.setConnectionTimeout(5000);
   int status = client.executeMethod(filePost);
   if (status == HttpStatus.SC_OK) {
      System.out.println("Upload complete, response=" + filePost.getResponseBodyAsString());
   }
   else {
      System.out.println("Upload failed, response=" + HttpStatus.getStatusText(status));
   }
}
catch (Exception ex) {
   System.out.println("Error: " + ex.getMessage());
   ex.printStackTrace();
}
finally {
   filePost.releaseConnection();
}


public void doPost(HttpServletRequest req, HttpServletResponse res) {

   try {

      DiskFileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);
      //upload.setSizeMax(100000000);

      // maximum size that will be stored in memory
      //factory.setSizeThreshold(1096);
      // the location for saving data that is larger than getSizeThreshold()
      //factory.setRepository(new File("/tmp"));

      List items = upload.parseRequest(req);

      String fileName = "";
      long maxSize = 0;

      Iterator iter = items.iterator();
      while (iter.hasNext()) {
         DiskFileItem item = (DiskFileItem) iter.next();

         // filename on the client
         fileName = item.getName();
         maxSize = item.getSize();

         System.out.println("fileName: " + fileName);
         System.out.println("maxSize: " + maxSize);

         String dest = "/tmp";

         // write the file
         item.write(new File(dest, fileName));
       }
   }
   catch(Exception e) {
         System.out.println("<br>\nError: "+e.getMessage());
      }
   }
}

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

I'm sorry, I'm not sure I understand.

What you are doing - is that you are developing an application
which would be deployed using WebStart ?

Or you are developing a servlet?

If you are developing a application which uses a Webstart - then
it is a regular application running on the client, the progress bar and everything
should work in the same way as with any other client-side
application.

If you are developing the servlet and your activity - uploading of the file
happens to be on the server side, then it is whole different thing,
I'm sure you can do something to keep the client informed
about your progres on the server activity,
but that would be quite different operation altogether.
 
please clarify
what is it that you need, as you talk about doPost method on one side (which normally
runs on server) and about JNLP on the other side (which is
used to deploy client-side applications).






Avatar of mock5c
mock5c

ASKER

I'm developing a webstart application that runs on client-side.  It processes many files.  This webstart then has an upload button to upload the files to a remote server, where the servlet resides.  So I am also developing a servlet.

I already have the webstart and servlet completed, that is the webstart successfully uploads the file to the server (see code I previously posted).  However, I am not sure how to include a progress monitor since the servlet, I assume, would monitor how many bytes are received and report this back to the webstart.

I hope that clarifies my question.



ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Well, of course servlet may know better how many dytes were already uploaded
and it is possible to devise some system to communicate back to client
what is the number of bytes which were uploaded
and then use progressbar code like the
one posted above by objects to show it.

In my opinion, frankly, that would be a little bit of an overkill to set up all this communication
back to client just for the sake of showing the progressbar.
I you are uploading really big files and the speeds of the
upload vary dramaticlly, then it is probably worth
doing. Otherwise I'd rather write some warning to user that
it will take some time, or make an estimate based on
the size of the initial file known to the client
and use this estimate of total time to show the
progress bar. Well, maybe some people will diagree with me, but I'd
think twice before complicating the system and adding
additional communications just because of the progress bar.
 

Avatar of mock5c

ASKER

Yes, files are quite large...usually 500MB - 1GB or more.  That is why I'm hoping to show a progress monitor.  I do have 2 progress monitors already used by the webstart but nothing with the servlet reporting current progress.

I'm sure you can do it somehow with servlet/client intercation
What comes to my mind though is to have an RMI server - which would run on
the server and you can reach to it from the client from time to time
and know how much data was already stored on the server

You can probably do something also with WebServices