FTP progress bar uploading file

梦想的初衷 提交于 2020-01-23 18:56:06

问题


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!



来源:https://stackoverflow.com/questions/11883636/ftp-progress-bar-uploading-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!