Get file upload percent complete?

Posted on 2009-12-16
Last Modified: 2012-06-21

I have some code which uploads a photo to a PHP script (code pasted below).

I'd like to give the user some indication as to upload progress - is the code snippet below the place to do that?

Do I just simply use "filelen" and "bytesRead" like this:

    while (bytesRead > 0) {
        println("Uploading " + (bytesRead / filelen) + " percent complete...");

DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

File theFile = File(pathToPhoto); 

FileInputStream fis = new FileInputStream(new );  

long filelen = theFile.length();


int bytesAvailable = fis.available(); 

int bufferSize = Math.min(bytesAvailable, MAX_BUF); 

byte[] buffer = new byte[bufferSize]; 

int bytesRead =, 0, bufferSize); 

while (bytesRead > 0) { 

    dos.write(buffer, 0, bufferSize); 

    bytesAvailable = fis.available(); 

    bufferSize = Math.min(bytesAvailable, maxBufferSize); 

    bytesRead =, 0, bufferSize); 


Open in new window

Question by:DJ_AM_Juicebox
    LVL 5

    Accepted Solution

    Looks like a good place to me.

    - filelen is the total size of the file
    - Just sum up the values you get for bytesRead each time you called
    f.e. int totalRead = 0; ... totalRead += bytesRead ...
    - progress might be calculated like this ranging from 0 to 100: int progress = 100 * totalRead / filelen;

    If the file size is smaller than MAX_BUF your progress will have one step only.
    In this case you could reduce MAX_BUF if it's too large.
    LVL 40

    Assisted Solution

    Looking at your code, 'bytesRead' is the number of bytes read at a particular instant when file stream read is done.

    You need to put one more variable in place to just keep on adding 'bytesRead' value in it inside the loop. Some thing like

    int bytesRead =, 0, bufferSize);
    int totalBytesRead = bytesRead;
    while (bytesRead > 0) {
        dos.write(buffer, 0, bufferSize);
        bytesAvailable = fis.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead =, 0, bufferSize);
        totalBytesRead = totalBytesRead  + bytesRead;
        System.out.println("Uploading " + (totalBytesRead / filelen) + " percent complete...");  

    Let me know if it helped

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    This video teaches viewers about errors in exception handling.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now