JSch sftp上传/下载进度 [英] JSch sftp upload/download progress

查看:222
本文介绍了JSch sftp上传/下载进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JSch和Java的新手.我设法获得了一些代码并以某种方式理解了它,但是我只停留在一点上.以下代码从SSH服务器下载文件,但我需要显示文件复制百分比的进度表.我该怎么做.非常感谢您的帮助.

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.

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();
    }
}

推荐答案

There's 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) 

方法和 com.jcraft.jsch.SftpProgressMonitor 界面.在此示例代码(有点混乱)的底部,您将找到SftpProgressMonitor的实现,该实现使用其回调方法count(long)end()来操纵

methods 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),而在传输结束时会调用end().因此,SftpProgressMonitor的一个非常简单的实现可能是:

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:

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!");
    }
}

然后我将创建一个实例并将其传递给get()

I'd then create an instance of that and pass it to get()

sftpChannel.get(sourcePath, destPath, new SystemOutProgressMonitor());

这篇关于JSch sftp上传/下载进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆