java JSch sftp 上传/下载进度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7977844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSch sftp upload/download progress
提问by World
I am new to JSch and java. I managed to get some codes and understand it somehow, but i am stuck on one point. The following code downloads file from SSH server, but i am in need of the progress meter that shows percentage of file copied. HOw can i do it. I will be greatly appreciate your help.
我是 JSch 和 Java 的新手。我设法获得了一些代码并以某种方式理解了它,但我被困在了一点上。以下代码从 SSH 服务器下载文件,但我需要显示复制文件百分比的进度表。我该怎么做。我将不胜感激您的帮助。
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpProgressMonitor;
public class SFTPExample {
public static void main(String args[]) throws Exception {
String user = "root";
String password = "password";
String host = "192.168.0.5";
int port = 22;
String knownHostsFilename = "/home/world/.ssh/known_hosts";
String sourcePath = "/media/nfs/genotype.txt";
String destPath = "genotype.txt";
JSch jsch = new JSch();
jsch.setKnownHosts(knownHostsFilename);
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
session.connect();
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
System.out.println("Downloading test file");
sftpChannel.get(sourcePath, destPath);
sftpChannel.exit();
session.disconnect();
}
}
回答by Jon Lin
There's methods in the com.jcraft.jsch.ChannelSftpthat you use to pass in a callback. Look at the
com.jcraft.jsch.ChannelSftp中有一些方法可用于传递回调。看着那(这
void get(java.lang.String src, java.lang.String dst, SftpProgressMonitor monitor)
methods and com.jcraft.jsch.SftpProgressMonitorinterface. At the bottom of this Example Code(it's kind of messy), you'll find an implementation of SftpProgressMonitor that uses its callback methods count(long)
and end()
to manipulate a javax.swing.ProgressMonitor.
方法和com.jcraft.jsch.SftpProgressMonitor接口。在此示例代码的底部(有点乱),您将找到 SftpProgressMonitor 的实现,它使用其回调方法count(long)
并end()
操作javax.swing.ProgressMonitor。
count(long)
gets called periodically when there's some bytes that have been transfered, and end()
gets called when the transfer has ended. So a really simple imeplementation of SftpProgressMonitor could be:
count(long)
当传输了一些字节时会定期调用,并end()
在传输结束时调用。所以 SftpProgressMonitor 的一个非常简单的实现可能是:
public class SystemOutProgressMonitor implements SftpProgressMonitor
{
public SystemOutProgressMonitor() {;}
public void init(int op, java.lang.String src, java.lang.String dest, long max)
{
System.out.println("STARTING: " + op + " " + src + " -> " + dest + " total: " + max);
}
public boolean count(long bytes)
{
for(int x=0; x < bytes; x++) {
System.out.print("#");
}
return(true);
}
public void end()
{
System.out.println("\nFINISHED!");
}
}
I'd then create an instance of that and pass it to get()
然后我会创建一个实例并将其传递给 get()
sftpChannel.get(sourcePath, destPath, new SystemOutProgressMonitor());