Issue
I am new to JSch and Java. I managed to get some code and understand it somehow, but I am stuck on one point. The following code downloads file from SSH/SFTP server, but I am in need of the progress meter that shows percentage of file copied. How can I do it?
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();
}
}
Solution
There are overloads of the get
/put
methods in the com.jcraft.jsch.ChannelSftp
that you use to pass in a callback. Look at the
void get(java.lang.String src, java.lang.String dst, SftpProgressMonitor monitor)
method and com.jcraft.jsch.SftpProgressMonitor
interface. 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
.
count(long)
gets called periodically when there's some bytes that have been transferred, and end()
gets called when the transfer has ended. So a really simple implementation of SftpProgressMonitor
could be:
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()
sftpChannel.get(sourcePath, destPath, new SystemOutProgressMonitor());
Answered By - Jon Lin Answer Checked By - Timothy Miller (WPSolving Admin)