简体   繁体   English

java中的FTP apache commons进度条

[英]FTP apache commons progress bar in java

I'm working on a little program, which can upload a file to my FTP Server and do some other stuff with it.我正在开发一个小程序,它可以将文件上传到我的 FTP 服务器并用它做一些其他的事情。 Now... It all works, i'm using the org.apache.commons.net.ftp FTPClient class for uploading.现在...一切正常,我正在使用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();

The file which should uploaded, is named in hTextField1.应该上传的文件在 hTextField1 中命名。 Now... How do i add a progress bar?现在...如何添加进度条? I mean, there is no stream in ftp.storeFile... How do i handle this?我的意思是,ftp.storeFile 中没有流...我该如何处理?

Thanks for any help!谢谢你的帮助! :) :)

Greetings你好

You can do it using the CopyStreamListener , that according to Apache commons docs is the listener to be used when performing store/retrieve operations. 您可以使用CopyStreamListener来完成它,根据Apache commons,docs是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);

Hope this helps 希望这可以帮助

我有同样的问题,当我使用 IDEA 控制台运行我的代码时,processBar 冻结,但它在终端上工作正常,请问您最终是如何解决的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM