JSch sftp upload/download progress

旧街凉风 提交于 2019-12-02 19:24:42
Jon Lin

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) 

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

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

sftpChannel.get(sourcePath, destPath, new SystemOutProgressMonitor());
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!