Uploaded image for project: 'Commons FileUpload'
  1. Commons FileUpload
  2. FILEUPLOAD-180

If ServletFileUpload.setSizeMax() is set the values "bytesRead" and "contentLength" of the ProgressListeners update()-method are ZERO

    XMLWordPrintableJSON

Details

    Description

      I wrote a ProgressListener to visualize the upload of a file.
      The Listener works as expected as long as the sizeMax-value of the ServletFileUpload class is not explicitly set.
      If it is set the update()-method of the ProgressListener has ZERO-values for bytesRead and contentLength ... whats wrong here?

      UploadServlet.java
      ...
      FileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);
      upload.setHeaderEncoding(request.getCharacterEncoding());
      upload.setFileSizeMax(DEFAULT_SIZE_MAX);
      // if sizeMax is set the update()-method has ZERO values for bytesRead and contentLength
      //upload.setSizeMax(DEFAULT_SIZE_MAX);
      FileUploadListener listener = new FileUploadListener(request);
      upload.setProgressListener(listener);
      			
      List items = null;
      try {
         items = upload.parseRequest(request);
      }
      ...
      
      FileUploadListener.java
      public class FileUploadListener implements ProgressListener
      {
         private volatile long 
         bytesRead = 0L,
         contentLength = -100L,
         tenKBRead = -1L,
         startTime = System.currentTimeMillis();
      		
         public FileUploadListener(HttpServletRequest request) 
         {
            final HttpSession session = request.getSession();
            session.setAttribute("FILE_UPLOAD_LISTENER", this);			
         }
      	    	
         public void update(long aBytesRead, long aContentLength, int anItem)	    
         {
            long tenKB = aBytesRead / 10240;
            if (tenKBRead == tenKB)
               return;
            tenKBRead = tenKB;
            bytesRead = aBytesRead;
            if (contentLength != aContentLength)
               contentLength = aContentLength;
         }
      
         public long getBytesRead() 
         {
            return bytesRead;
         }
      
         public long getContentLength() 
         {
            return contentLength;
         }
      	    
         public long getElapsedTimeInSeconds()
         {
            return (System.currentTimeMillis() - startTime) / 1000;
         }
      }
      

      Attachments

        Activity

          People

            simone.tripodi Simone Tripodi
            hooorny Björn Agel
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: