javaspringapache-commons-fileupload

Commons file upload with ProgressListener gets more bytes than file actual size


When i uplaod a file use apache commons-fileupload, it works fine. But when i add a ProgressListener to monitor upload progress, the pBytesRead is more than pContentLength when the file is a compressed file, like a .zip file or .7z file, if i uncompress the zip file to a regular file, it works perfectlly.

Here is my ProgressListener:

package com.qzing.ieep.file.api.listener;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.apache.commons.fileupload.ProgressListener;
import org.springframework.stereotype.Component;

@Component
public class FileUploadProgressListener implements ProgressListener {

    public void update(long pBytesRead, long pContentLength, int pItems) {
        System.out.println(pBytesRead + "/" + pContentLength);
        BigDecimal percent = new BigDecimal(pBytesRead).divide(new BigDecimal(pContentLength), 2, RoundingMode.DOWN)
                .multiply(new BigDecimal("100"));
        if(percent.compareTo(new BigDecimal("100")) == 0) {
            System.out.println("Upload complete.");
        }
    }

}

When i upload a zip file, outputs:

1024/16494814
2048/16494814
...
16494814/16494814
Upload complete.
16495838/16494814
16495962/16494814
...
29866392/16494814
29870434/16494814
29873819/16494814

The pBytesRead is more than pContentLength, and the percentage is always around 182.

When i upload a .sql file, outputs:

1024/16494814
2048/16494814
...
16493790/16494814
16494814/16494814
Upload complete.

Any idea why this happened?


Solution

  • I resolved it. Turns out it's my own problem, I add a RequestWrapper to cache request body for logging and other things, and the MultipartRequest is also converted as a String to cache, the String's length is diffrent than the original reqeust body.