我正在尝试测试到达SFTP服务器的文件。我使用以下代码设置了本地sshd服务器
@Before
public void setUp() throws Exception{
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
public boolean authenticate(String username, String password, ServerSession session) {
return true;
}
});
sshd.setPort(22);
sshd.setFileSystemFactory(new FileSystemFactory() {
@Override
public FileSystem createFileSystem(org.apache.sshd.common.session.Session session) throws IOException {
return null;
}
});
sshd.start();
}
我使用Jsch作为客户端尝试连接到该服务器。不幸的是,每当我尝试调用channel.connect()时,都会出现com.jcraft.jsch.JSchException:无法发送频道请求异常。 Jsch的代码如下所示:
public void testServer(){
try{
JSch jSch = new JSch();
Session session = jSch.getSession("user", "localhost",22);
Properties configTemp = new Properties();
configTemp.put("StrictHostKeyChecking", "no");
session.setConfig(configTemp);
session.connect();
ChannelSftp channel = (ChannelSftp)session.openChannel("sftp");
channel.connect();
if(channel.isConnected()){
System.out.println("Connected");
}
}
catch (Exception e){
e.printStackTrace();
}
}
有人可以帮助我解决我做错的事情,以及如何解决此问题?谢谢
您必须实现一个子系统工厂,例如:
sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));