FTP progress bar uploading file

2019-06-01 18:36发布

问题:

Hi guys I am using apache.commons.net to upload my files from sd card to a ftp server created by file zilla. However, all I want to do is to show the progress to the users. Could you please help me? here is my code:

http://pastie.org/4433482

回答1:

if in case you never solve your problem yet. I think the problem is within this line publishProgress

((int) ((totalBytesTransferred/file.length())*100))

instead try this

publishProgress((int) ((totalBytesTransferred * 100)/file.length()))


回答2:

There is a problem with this line:

publishProgress((int) ((totalBytesTransferred/file.length())*100));

Since totalBytesTransferred is long and File.length() returns long, and integer division will be performed. So this line will return zero until totalBytesTransferred equals file.length(). It will then return 100.

You could cast totalBytesTransferred to double before dividing like this to get the percentage:

publishProgress((int) (((double)totalBytesTransferred/file.length())*100));


回答3:

On line 322 of your paste.

org.apache.commons.net.io.Util.copyStream(stO, stD, ftpClient.getBufferSize(),
      CopyStreamEvent.UNKNOWN_STREAM_SIZE,
      new CopyStreamAdapter() {
          public void bytesTransferred(
                   long totalBytesTransferred,
                   int bytesTransferred,
                   long streamSize) {
                      // Your progress Control code here
                      Log.d("CopyStreamAdapter", "bytesTransferred(...) - " +
                            totalBytesTransferred + "; " +
                            bytesTransferred + "; " + 
                            streamSize);
                      publishProgress((int) ((totalBytesTransferred/file.length())*100));
                   }
           }
      );

This I suspect is where its failing! If you're not getting any logcat with the string "CopyStreamAdapter", it means your handler in this is not getting fired!