You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Thomas Neidhart (JIRA)" <ji...@apache.org> on 2013/03/10 12:09:13 UTC

[jira] [Commented] (FILEUPLOAD-180) If ServletFileUpload.setSizeMax() is set the values "bytesRead" and "contentLength" of the ProgressListeners update()-method are ZERO

    [ https://issues.apache.org/jira/browse/FILEUPLOAD-180?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13598208#comment-13598208 ] 

Thomas Neidhart commented on FILEUPLOAD-180:
--------------------------------------------

I could not yet reproduce the problem, see my test case below:

{noformat}
    @Test
    public void testProgressListener()
            throws IOException, FileUploadException {
        final String request =
            "-----1234\r\n" +
            "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
            "Content-Type: text/whatever\r\n" +
            "Content-Length: 10\r\n" +
            "\r\n" +
            "This is the content of the file\n" +
            "\r\n" +
            "-----1234--\r\n";

        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
        upload.setFileSizeMax(Long.MAX_VALUE);
        HttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);

        FileUploadListener listener = new FileUploadListener();
        upload.setProgressListener(listener);
        
        List<FileItem> fileItems = upload.parseRequest(req);
        assertEquals(1, fileItems.size());
        FileItem item = fileItems.get(0);
        assertEquals("This is the content of the file\n", new String(item.get()));
    }

    public class FileUploadListener implements ProgressListener
    {
       public void update(long aBytesRead, long aContentLength, int anItem)     
       {
           assertTrue(aBytesRead > 0);
           System.out.println(aBytesRead + " " + aContentLength + " " + anItem);
       }
    }
{noformat}

The result is:

{noformat}
174 -1 0
174 -1 1
{noformat}

This bytesRead in the update method is correctly set. Maybe it is related to your environment, could you come up with a very simple webapp test case to demonstrate this?
                
> If ServletFileUpload.setSizeMax() is set the values "bytesRead" and "contentLength" of the ProgressListeners update()-method are ZERO
> -------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: FILEUPLOAD-180
>                 URL: https://issues.apache.org/jira/browse/FILEUPLOAD-180
>             Project: Commons FileUpload
>          Issue Type: Bug
>    Affects Versions: 1.2.2
>         Environment: Tomcat 6.0.24
>            Reporter: Björn Agel
>              Labels: Zero, bytesRead, contentLength, fileupload, progress_listener, sizeMax
>
> 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?
> \\
> \\
> {code:title=UploadServlet.java|borderStyle=solid}
> ...
> 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);
> }
> ...
> {code} 
> {code:title=FileUploadListener.java|borderStyle=solid}
> 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;
>    }
> }
> {code} 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira