Fixed the problem of progress indicator on file upload progress bar

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-gwt-service@85240 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2013-11-08 16:33:05 +00:00
parent 2e68827683
commit 5b68d9fb5d
3 changed files with 72 additions and 23 deletions

View File

@ -76,7 +76,7 @@ public class LocalUploadServlet extends HttpServlet {
ServletFileUpload upload = new ServletFileUpload(factory); ServletFileUpload upload = new ServletFileUpload(factory);
FileUploadListener uploadListener = new FileUploadListener(fileUploadSession.getFileUploadMonitor()); FileUploadListener uploadListener = new FileUploadListener(fileUploadMonitor);
upload.setProgressListener(uploadListener); upload.setProgressListener(uploadListener);
FileItem uploadItem = null; FileItem uploadItem = null;

View File

@ -3,8 +3,14 @@
*/ */
package org.gcube.portlets.user.td.gwtservice.server.file; package org.gcube.portlets.user.td.gwtservice.server.file;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.ProgressListener; import org.apache.commons.fileupload.ProgressListener;
import org.gcube.portlets.user.td.gwtservice.server.SessionUtil;
import org.gcube.portlets.user.td.gwtservice.shared.file.FileUploadMonitor; import org.gcube.portlets.user.td.gwtservice.shared.file.FileUploadMonitor;
import org.gcube.portlets.user.td.gwtservice.shared.file.FileUploadState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* *
@ -13,19 +19,55 @@ import org.gcube.portlets.user.td.gwtservice.shared.file.FileUploadMonitor;
* *
*/ */
public class FileUploadListener implements ProgressListener { public class FileUploadListener implements ProgressListener {
protected Logger logger=LoggerFactory.getLogger(FileUploadListener.class);
protected HttpSession session;
protected FileUploadMonitor fileUploadMonitor; protected FileUploadMonitor fileUploadMonitor;
private long num100Ks = 0;
private long theBytesRead = 0;
private long theContentLength = -1;
private int whichItem = 0;
private int percentDone = 0;
private boolean contentLengthKnown = false;
public FileUploadListener(FileUploadMonitor fileUploadMonitor) { public FileUploadListener(FileUploadMonitor fileUploadMonitor) {
this.fileUploadMonitor = fileUploadMonitor; this.fileUploadMonitor=fileUploadMonitor;
//this.session=session;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public void update(long pBytesRead, long pContentLength, int pItems) { public void update(long bytesRead, long contentLength, int items) {
fileUploadMonitor.setTotalLenght(pContentLength); if (contentLength > -1) {
fileUploadMonitor.setElaboratedLenght(pBytesRead); contentLengthKnown = true;
}
theBytesRead = bytesRead;
theContentLength = contentLength;
whichItem = items;
long nowNum100Ks = bytesRead / 100000;
// Only run this code once every 100K
if (nowNum100Ks > num100Ks) {
num100Ks = nowNum100Ks;
if (contentLengthKnown) {
percentDone = (int) Math.round(100.00 * bytesRead / contentLength);
}
}
//FileUploadSession fileUploadSession=SessionUtil.getFileUploadSession(session);
//FileUploadMonitor fileUploadMonitor=fileUploadSession.getFileUploadMonitor();
fileUploadMonitor.setTotalLenght(contentLength);
fileUploadMonitor.setElaboratedLenght(bytesRead);
fileUploadMonitor.setPercentDone(Float.valueOf(percentDone)/100);
fileUploadMonitor.setState(FileUploadState.INPROGRESS);
logger.debug("File Upload: "+fileUploadMonitor.toString());
} }
} }

View File

@ -20,6 +20,7 @@ public class FileUploadMonitor implements Serializable {
protected FileUploadState state; protected FileUploadState state;
protected String failureReason; protected String failureReason;
protected String failureDetails; protected String failureDetails;
protected float percentDone;
public FileUploadMonitor(){ public FileUploadMonitor(){
state = FileUploadState.INPROGRESS; state = FileUploadState.INPROGRESS;
@ -30,6 +31,7 @@ public class FileUploadMonitor implements Serializable {
this.elaboratedLenght = elaboratedLenght; this.elaboratedLenght = elaboratedLenght;
this.state = state; this.state = state;
this.failureReason = failureReason; this.failureReason = failureReason;
this.percentDone=0;
} }
/** /**
@ -58,6 +60,22 @@ public class FileUploadMonitor implements Serializable {
return failureDetails; return failureDetails;
} }
public float getPercentDone() {
return percentDone;
}
public void setPercentDone(float percentDone) {
this.percentDone = percentDone;
}
public void setFailureReason(String failureReason) {
this.failureReason = failureReason;
}
public void setFailureDetails(String failureDetails) {
this.failureDetails = failureDetails;
}
public void setState(FileUploadState state) public void setState(FileUploadState state)
{ {
this.state = state; this.state = state;
@ -93,24 +111,13 @@ public class FileUploadMonitor implements Serializable {
this.failureDetails = failureDetails; this.failureDetails = failureDetails;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); return "FileUploadMonitor [totalLenght=" + totalLenght
builder.append("OperationProgress [totalLenght="); + ", elaboratedLenght=" + elaboratedLenght + ", state=" + state
builder.append(totalLenght); + ", failureReason=" + failureReason + ", failureDetails="
builder.append(", elaboratedLenght="); + failureDetails + ", percentDone=" + percentDone + "]";
builder.append(elaboratedLenght);
builder.append(", state=");
builder.append(state);
builder.append(", failureReason=");
builder.append(failureReason);
builder.append(", failureDetails=");
builder.append(failureDetails);
builder.append("]");
return builder.toString();
} }
} }