简体   繁体   English

sftp服务器中的连接被拒绝

[英]Connection refused in sftp server

I am trying to use localhost as SFTP server. 我正在尝试使用localhost作为SFTP服务器。 I'm using Jsch library of Java to implement SFTP with SSH2. 我正在使用Java的Jsch库通过SSH2实现SFTP。 The following code upload a text file to a directory on local machine with sftp. 以下代码使用sftp将文本文件上传到本地计算机上的目录中。 But I cannot connect to localhost. 但是我无法连接到本地主机。

        import java.io.BufferedInputStream;
        import java.io.BufferedOutputStream;
        import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileOutputStream;
        import java.io.OutputStream;


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


        public class  Server{

        public Server() {
        }

        public static void main(String[] args) {
        String SFTPHOST = "localhost";
        int    SFTPPORT = 22;
        String SFTPUSER = "root";
        String SFTPPASS ="";
        String SFTPWORKINGDIR = "D:\\Upload";

        Session     session     = null;
        Channel     channel     = null;
        ChannelSftp channelSftp = null;

        try{
                    JSch jsch = new JSch();
                    session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
                    session.setPassword(SFTPPASS);
                    java.util.Properties config = new java.util.Properties();
                    config.put("StrictHostKeyChecking", "no");
                    session.setConfig(config);
                    session.connect();
                    channel = session.openChannel("sftp");
                    channel.connect();
                    channelSftp = (ChannelSftp)channel;
                    channelSftp.cd(SFTPWORKINGDIR);
                    File f = new File("trial.txt");
                    channelSftp.put(new FileInputStream(f), f.getName());
        }catch(Exception ex){
        ex.printStackTrace();
        }

        }

        }

"Connection refused" means that there is no process listening for connections at the IP address and port which you're trying to connect to. “拒绝连接”表示没有进程在尝试连接的IP地址和端口上监听连接。 In this case, the most likely explanation is that your SSH server process isn't running, or perhaps it has been configured to listen on some other port than port 22. 在这种情况下,最可能的解释是您的SSH服务器进程未运行,或者可能已配置为侦听端口22以外的其他端口。

It looks like this is a Windows system? 看起来这是Windows系统吗? If that's the case, you could check the task manager to see if there is a copy of the sshd program running. 如果是这样,您可以检查任务管理器以查看是否正在运行sshd程序的副本。 To see if something is listening on port 22, open a command-line window and run netstat -an . 要查看端口22上是否有监听的内容,请打开命令行窗口,然后运行netstat -an Then look for a line like this: 然后寻找这样的一行:

  TCP    0.0.0.0:22            0.0.0.0:0              LISTENING
                 ^^-- Listening on port 22

If sshd isn't running, it needs to be started. 如果sshd没有运行,则需要启动它。 If it is running, but nothing is listening on port 22, then you should examine sshd's configuration file. 如果它正在运行,但是端口22上没有监听,那么您应该检查sshd的配置文件。

If you need further help starting sshd, you should ask on https://superuser.com/ . 如果您需要进一步的帮助来启动sshd,请在https://superuser.com/上询问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM