1

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

http://pastie.org/4433482

4

3 回答 3

2

如果你还没有解决你的问题。我认为问题出在这一行 publishProgress

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

试试这个

publishProgress((int) ((totalBytesTransferred * 100)/file.length()))
于 2012-10-31T07:33:29.187 回答
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));
于 2012-08-09T12:56:20.097 回答
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,这意味着你的处理程序没有被解雇!

于 2012-08-09T14:11:05.723 回答