Search code examples
javaprogressapache-commons-fileupload

How to use commons progress update to multi user environment?


Now i am uploading files with servlet and update progress with implementing ProgressListener class. The class that implements ProgressListener Static Variables. How shall i move the variables to an object and make it unique for each user.

The Class Implementing ProgressListener:

package com.pricar.uploadlistener.pack;

import java.text.NumberFormat;

import org.apache.commons.fileupload.ProgressListener;

public class FileUploadProgressListener implements ProgressListener {

public FileUploadProgressListener() {
}
private static long bytesRead;
private static long totalBytes;
public void getFileUploadStatus() {

    String per = NumberFormat.getPercentInstance().format( (double) bytesRead / (double) totalBytes );
    String statusStr = (per.substring(0, per.length() - 1));
    int status = Integer.parseInt(statusStr);
}

/* (non-Javadoc)
 * @see org.apache.commons.fileupload.ProgressListener#update(long, long, int)
 */
public void update(long pBytesRead, long pContentLength, int pItems) {

    bytesRead = pBytesRead;
    totalBytes = pContentLength;

    }
}

Any Suggestions or Link would be more appreciative!!

Thanks!!


Solution

  • I would make bytesRead and totalBytes local attributes, so each instance of FileUploadProgressListener has its own data. Then store that instance in a session variable so every user gets their own.