FTP progress bar uploading file

2019-06-01 18:52发布

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

3条回答
Explosion°爆炸
2楼-- · 2019-06-01 19:17

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楼-- · 2019-06-01 19:30

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()))
查看更多
我命由我不由天
4楼-- · 2019-06-01 19:38

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!

查看更多
登录 后发表回答