FTP进度条上传文件

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

http://pastie.org/4433482

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

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

而是试试这个

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

这条线有问题:

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

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

您可以将totalBytesTransferred强制转换为double,然后再按这样划分以获得百分比:

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

在粘贴的第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,这意味着你的处理程序不会被解雇!