4

I have found this code sample

import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;


public class PostFile {
  public static void main(String[] args) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    HttpPost httppost = new HttpPost("http://localhost:9001/upload.php");
    File file = new File("c:/TRASH/zaba_1.jpg");

    MultipartEntity mpEntity = new MultipartEntity();
    ContentBody cbFile = new FileBody(file, "image/jpeg");
    mpEntity.addPart("userfile", cbFile);


    httppost.setEntity(mpEntity);
    System.out.println("executing request " + httppost.getRequestLine());
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();

    System.out.println(response.getStatusLine());
    if (resEntity != null) {
      System.out.println(EntityUtils.toString(resEntity));
    }
    if (resEntity != null) {
      resEntity.consumeContent();
    }

    httpclient.getConnectionManager().shutdown();
  }
}

I am just wondering how to get uploaded bytes sum?

user592704
  • 3,674
  • 11
  • 70
  • 107
  • Are you looking for this size of the `File`, or the size of the entire POST (which will only be a little bit bigger)? – Matt Ball Mar 14 '11 at 03:54
  • No, I am looking to get how many bytes were uploaded. To show the number in console – user592704 Mar 14 '11 at 06:47
  • possible duplicate of [File Upload with Java (with progress bar)](http://stackoverflow.com/questions/254719/file-upload-with-java-with-progress-bar) – BalusC Mar 16 '11 at 17:13

1 Answers1

5

Override FileBody.writeTo(OutputStream) to counts bytes as they are written. This allows a count of bytes sent (even if interrupted), both during the upload and after it is complete.

public class FileBodyCounter extends FileBody {
    private volatile long byteCount;

    public long getBytesWritten() {
        return byteCount;
    }

    public void writeTo(OutputStream out) {
        super.writeTo(new FilterOutputStream(out) {
            // Other write() methods omitted for brevity. Implement for better performance
            public void write(int b) throws IOException {
                byteCount++;
                super.write(b);
            }
        });
    }
}

Use that instead of the standard FileBody, and retrieve the bytecount during upload, or after the post has completed.

Chadwick
  • 12,555
  • 7
  • 49
  • 66
  • I want to see the uploading process numbers. I mean not after but within :) – user592704 Mar 14 '11 at 06:49
  • As stated above, you can access the bytes written count during the upload. Simply allow the upload to proceed on one thread, and call the `FileBodyCounter`'s `getBytesWritten()` method on another thread. – Chadwick Mar 14 '11 at 11:19
  • Thanks I will, but I was interested in MultiPartEntity to get the upload progress. I have found many examples :) – user592704 Mar 18 '11 at 01:51