You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by εδΈ‰ιƒŽ <mi...@163.com> on 2013/08/12 08:12:21 UTC

[fileupload]how to get progress stats from diffrent request.

   if i upload only one file,i save the percent stats to session,i can get the currect stats from ProgressListener.

  if i upload two file f1,f2 in same session,but diffrent request.i cant distinguish the two stats.
  to solve this problem,
    1.i add a text field named "key" to distinguish two form action.
    2.pass value of "key" to ProgressListener.

  for example:
  i will post in diffrent request:
    ----------
    <form method=post enctype=multipart/form-data>
      <input type=text name=key value=f1 >
      <input type=file name=file1 >
    </form>
    ----------
    <form method=post enctype=multipart/form-data>
      <input type=text name=key value=f2 >
      <input type=file name=file2 >
    </form>

  i define my ProgressListener:
   public class UploadPro implements ProgressListener {
    private HttpServletRequest request;
    private String keyi;
    private DecimalFormat df = new DecimalFormat("#00.0");    //"#00.0"
    public UploadPro(HttpServletRequest request,String key){  
        this.request = request;
        this.keyi=keyi;
    }
    @Override
    public void update(long bytesRead, long bytesTotal, int items) {  
        double percent= (double)bytesRead*100/(double)bytesTotal;
        System.out.println("percent :"+df.format(percent));
        request.getSession().setAttribute("UPLOAD_PERCENTAGE_"+key, df.format(percent));  
    }
  }
  thus,the diffrent progress stats saved in diffrent session  variable("UPLOAD_PERCENTAGE_"+key).



  to pass key value to ProgressListener,i must get value of parameter "key" before setProgressListener.
  my code write like this:
  ...
  List<FileItem> fileItems = upload.parseRequest(request);
  FileItem key=fileItems.get(0);            
  upload.setProgressListener(new UploadPro(request,key.getString));
  ...
 
  i run the project,i find ProgressListener do not work.and no exception message printed.
  so,i find ProgressListener not work if method setProgressListener called after method parseRequest.
  if setProgressListener before parseRequest,the code run correct,but i cant get parameter value.

  so, how to get progress stats from diffrent request(same session)????
  thanks very much!!!