JSch - 如何确定会话是否已打开?

时间:2014-10-14 01:12:29

标签: java eclipse ssh jsch

我有一个可运行的SSH连接并将文件从服务器传输到本地。问题是我无法正确地弄清楚如何判断是否已经存在活动会话。每次运行以下代码时,getSession()的值为null。我无法弄清楚我哪里出错了,过去几个小时我一直在盯着这段代码。任何帮助表示赞赏。

  • 在旁注中,我还需要弄清楚如何关闭来自另一个类的连接(即主要活动中的MenuItem,用户单击以关闭连接)。由于我甚至无法弄清楚如何获得当前的JSch会话,因此它有点超出我的范围。对此的任何帮助也将不胜感激。

我的代码如下:

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class SCPReceive implements Runnable {

private JSch jsch;
private ChannelSftp sftpChannel;
private Session session;

private String username;
private String password;
private String host;
private int port;

private volatile static SCPReceive instance = null;

SCPReceive() {

    this.jsch = new JSch();
    this.username = "username";
    this.password = "password";
    this.host = "server.com";
    this.port = 22;


}


public static SCPReceive getInstance() {

    if (SCPReceive.instance == null) {

        synchronized (SCPReceive.class) {

            if (SCPReceive.instance == null) {

                SCPReceive.instance = new SCPReceive();

            }

        }

    }

    return SCPReceive.instance;

}

public void run() {


    if (null == getSession() || !getSession().isConnected()) {

        try {
        System.out.println("Opening SFTP Connection");
        setSession(jsch.getSession(this.username, this.host, this.port));
        getSession().setConfig("StrictHostKeyChecking", "no");
        getSession().setPassword(this.password);
        getSession().connect();

        Channel channel = getSession().openChannel("sftp");
        channel.connect();
        setSftpChannel((ChannelSftp) channel);
        } catch(Exception e){
            System.out.println("Error on connecting to server: " + e);
        }

        try {
            get();
        } catch (SftpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    } else {

        System.out.println("SFTP Connection already open");

    }

    System.out.println("Success");

}


public void closeConnection() {

    System.out.println("Closing SFTP connection");
    getSftpChannel().exit();
    getSession().disconnect();
    System.out.println("SFTP Connection closed");

}

public boolean isOpen() {

    if (getSession().isConnected()) {

        return true;

    } else {

        return false;

    }

}

private ChannelSftp getSftpChannel() {

    return this.sftpChannel;

}

private Session getSession() {

    return this.session;

} 

private void setSftpChannel(ChannelSftp sftpChannel) {

    this.sftpChannel = sftpChannel;

}

private void setSession(Session session) {

    this.session = session;

}


public void get() throws SftpException {
    String remote = "/path/to/file/1.mp4";
    String local = "/save/path/to/f1le/1.mp4";

    System.out.println("Retrieving file: " + remote + " saving to: " + local);
    getSftpChannel().get(remote, local);
    System.out.println("Success");

}

}

编辑:  runnable通过以下方式调用:

            @Override
        public void actionPerformed(ActionEvent e) {
            SCPReceive t = new SCPReceive();
            Thread SCPReceive_thread = new Thread(t);
            SCPReceive_thread.start();

        }
    });

0 个答案:

没有答案