FTP进度条上传文件

时间:2012-08-09 12:37:46

标签: java android ftp progress-bar

大家好我正在使用apache.commons.net将我的文件从SD卡上传到由zilla创建的ftp服务器。但是,我想要做的就是向用户显示进度。请你帮助我好吗? 这是我的代码:

http://pastie.org/4433482

3 个答案:

答案 0 :(得分:2)

如果您还没有解决问题。我认为问题在于这一行 publishProgress

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

而不是试试这个

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

答案 1 :(得分:0)

此行存在问题:

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

由于totalBytesTransferred为long且File.length()返回long,因此将执行整数除法。因此,此行将返回零,直到totalBytesTransferred等于file.length()。然后它将返回100。

你可以将totalBytesTransferred转换为double,然后再像这样划分得到百分比:

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

答案 2 :(得分:0)

粘贴的第322行。

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));
                   }
           }
      );

我怀疑这是失败的地方!如果你没有得到任何带有字符串“ CopyStreamAdapter ”的logcat,这意味着你的处理程序不会被解雇!

相关问题