Announcement Announcement Module
Collapse
No announcement yet.
File Upload with progress bar Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • File Upload with progress bar

    How can I get inputStream for file? I am using Commons file upload. And I need bytes count before it finish save file to temp dir.

  • #2
    My co-worker did some investigating work with this using AJAX and the workflow was something like:

    - upload the file in one request
    - server intercepts the upload, determines the size of the file from the headers
    - in a seperate request ask the server how many bytes have been uploaded.

    The key is to uniquely identify the file being uploaded. The way my co-worker did it was to assume one file being uploaded per http session, so the uploading code updated the number of bytes/total number under a known key in the session.

    That is a bit lame, but sufficient as a proof of concept

    He did have to modify the commons upload code, but it was only a few lines and he found it very easy.

    Sorry I cannot be more help, but I wasn't involved

    Comment


    • #3
      Working! I remove multipartResolver from my xml file.

      Comment


      • #4
        could you post some piece of your solution, or explain how you did it ?
        I'm searching for an ajax-file-upload solution ready out of the box, so that i can replace my uploadFileController with an ajaxUploadFileController.

        thanks anyway,
        valerio

        Comment


        • #5
          Sorry for my English, I lust post my code:

          in FileUploadController:
          Code:
          ...
          byte[] buffer = new byte[8 * 1024];
                              MonitoredFileOutputStream fileOutputStream = new MonitoredFileOutputStream(filePath + filename, new UploadListenerFile(request, 0l));
                              while ((i = inputStream.read(buffer)) != -1) {
                                  fileOutputStream.write(buffer, 0, i);
                              }
                              fileOutputStream.flush();
                              fileOutputStream.close();
          ...
          MonitoredFileOutputStream
          Code:
          public class MonitoredFileOutputStream extends FileOutputStream {
              private FileOutputStreamListener listener;
          
              public MonitoredFileOutputStream(String name, FileOutputStreamListener listener) throws FileNotFoundException {
                  super(name);
                  this.listener = listener;
                  this.listener.start();
              }
          
              public void write(byte b[], int off, int len) throws IOException {
                  super.write(b, off, len);
                  listener.bytesRead(len - off);
              }
          
              public void write(byte b[]) throws IOException {
                  super.write(b);
                  listener.bytesRead(b.length);
              }
          
              public void write(int b) throws IOException {
                  super.write(b);
                  listener.bytesRead(1);
              }
          
              public void close() throws IOException {
                  super.close();
                  listener.done();
              }
          
              public void flush() throws IOException {
                  super.flush();
              }
          }
          FileOutputStreamListener
          Code:
          public interface FileOutputStreamListener {
              public void start();
              public void bytesRead(int bytesRead);
              public void error(String message);
              public void done();
          }
          UploadListenerFile
          Code:
          public class UploadListenerFile implements FileOutputStreamListener {
              private HttpServletRequest request;
              private long delay = 0;
              private long startTime = 0;
              private int totalToRead = 0;
              private int totalBytesRead = 0;
              private int totalFiles = -1;
          
              public UploadListenerFile(HttpServletRequest request, long debugDelay) {
                  this.request = request;
                  this.delay = debugDelay;
                  totalToRead = request.getContentLength();
                  this.startTime = System.currentTimeMillis();
              }
          
              public void start() {
                  totalFiles ++;
                  updateUploadInfo("start");
              }
          
              public void bytesRead(int bytesRead) {
                  totalBytesRead = totalBytesRead + bytesRead;
                  updateUploadInfo("progress");
          
                  try {
                      Thread.sleep(delay);
                  }
                  catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
          
              public void error(String message) {
                  updateUploadInfo("error");
              }
          
              public void done() {
                  updateUploadInfo("done");
              }
          
              private long getDelta() {
                  return (System.currentTimeMillis() - startTime) / 1000;
              }
          
              private void updateUploadInfo(String status) {
                  long delta = (System.currentTimeMillis() - startTime) / 1000;
                  request.getSession().setAttribute("uploadInfo", new UploadInfo(totalFiles, totalToRead, totalBytesRead, delta, "temp"));
              }
          
          }
          UploadInfo
          Code:
          public class UploadInfo {
              private long totalSize = 0;
              private long bytesRead = 0;
              private long elapsedTime = 0;
              private String status = "done";
              private int fileIndex = 0;
          
              public UploadInfo() {
              }
          
              public UploadInfo(int fileIndex, long totalSize, long bytesRead, long elapsedTime, String status) {
                  this.fileIndex = fileIndex;
                  this.totalSize = totalSize;
                  this.bytesRead = bytesRead;
                  this.elapsedTime = elapsedTime;
                  this.status = status;
              }
          
              public String getStatus() {
                  return status;
              }
          
              public void setStatus(String status) {
                  this.status = status;
              }
          
              public long getTotalSize() {
                  return totalSize;
              }
          
              public void setTotalSize(long totalSize) {
                  this.totalSize = totalSize;
              }
          
              public long getBytesRead() {
                  return bytesRead;
              }
          
              public String getBytesReadFormated() {
                 return FileUtils.byteCountToDisplaySize(bytesRead);
              }
          
              public void setBytesRead(long bytesRead) {
                  this.bytesRead = bytesRead;
              }
          
              public long getElapsedTime() {
                  return elapsedTime;
              }
          
              public void setElapsedTime(long elapsedTime) {
                  this.elapsedTime = elapsedTime;
              }
          
              public int getPercent() {
                  if (totalSize > 0)
                  return Math.round(bytesRead * 100 / totalSize);
                  else return 0;
              }
          
              public boolean isInProgress() {
                  return "progress".equals(status) || "start".equals(status);
              }
          
              public int getFileIndex() {
                  return fileIndex;
              }
          
              public void setFileIndex(int fileIndex) {
                  this.fileIndex = fileIndex;
              }
          }

          Comment

          Working...
          X