【问题标题】:FTP apache commons progress bar in javajava中的FTP apache commons进度条
【发布时间】:2013-02-24 22:02:02
【问题描述】:

我正在开发一个小程序,它可以将文件上传到我的 FTP 服务器并用它做一些其他事情。 现在...一切正常,我正在使用org.apache.commons.net.ftp FTPClient 类进行上传。

ftp = new FTPClient();
ftp.connect(hostname);
ftp.login(username, password);

ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.changeWorkingDirectory("/shares/public");
int reply = ftp.getReplyCode();

if (FTPReply.isPositiveCompletion(reply)) {
    addLog("Uploading...");
} else {
    addLog("Failed connection to the server!");
}

File f1 = new File(location);
in = new FileInputStream(

ftp.storeFile(jTextField1.getText(), in);

addLog("Done");

ftp.logout();
ftp.disconnect();

应该上传的文件在 hTextField1 中命名。 现在...如何添加进度条?我的意思是,ftp.storeFile 中没有流...我该如何处理?

感谢您的帮助! :)

问候

【问题讨论】:

    标签: java ftp progress-bar apache-commons-net


    【解决方案1】:

    您可以使用CopyStreamListener,根据 Apache commons 文档,the listener to be used when performing store/retrieve operations.

    CopyStreamAdapter streamListener = new CopyStreamAdapter() {
    
        @Override
        public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) {
           //this method will be called everytime some bytes are transferred
    
           int percent = (int)(totalBytesTransferred*100/yourFile.length());
           // update your progress bar with this percentage
        }
    
     });
    ftp.setCopyStreamListener(streamListener);
    

    希望对你有帮助

    【讨论】:

    • 哦,非常感谢,这行得通!但是现在我遇到了另一个问题......如果我按下上传文件的按钮,程序会冻结,如果上传完成,完整的进度条已满......
    • 那是因为您在 GUI 线程中进行上传,所以 GUI 冻结,并等待您的上传完成,避免这种情况的方法是使用 Threads,有一个例子:@ 987654323@
    • 经过数小时的搜索,终于找到了适合我的解决方案。 +1!谢谢@BackSlash!
    猜你喜欢
    • 2014-08-09
    • 2011-10-02
    • 1970-01-01
    • 2015-10-11
    • 2015-01-24
    • 2013-06-17
    • 1970-01-01
    • 2011-08-18
    相关资源
    最近更新 更多