我正在尝试使用 Apache Mina 和 JSch 编写单元测试,但我遇到了一个问题,我确信这与我在 Mina 上设置文件系统的方式有关。
这是Mina 设置代码:
sshd = SshServer.setUpDefaultServer();
sshd.setPort(8002);
sshd.setFileSystemFactory(new NativeFileSystemFactory() {
@Override
public void setCreateHome(boolean createHome)
{
super.setCreateHome(true);
}
@Override
public FileSystemView createFileSystemView(final Session session) {
String userName = session.getUsername();
// create home if does not exist
String homeDirStr = "/home/testusr";
File homeDir = new File(homeDirStr);
if ((!homeDir.exists()) && (!homeDir.mkdirs())) {
System.out.println("Cannot create user home :: " + homeDirStr);
}
return new NativeFileSystemView(session.getUsername(), false) {
@Override
public String getVirtualUserDir() {
return "/home/testusr";
}
};
};
});
sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
sshd.setCommandFactory(new ScpCommandFactory());
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
userAuthFactories.add(new UserAuthNone.Factory());
sshd.setUserAuthFactories(userAuthFactories);
sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {
public boolean authenticate(String username, PublicKey key, ServerSession session) {
return true;
}
});
sshd.start();
JS代码:
JSch jsch = new JSch();
String appPublicKey = "c:\\conf\\test_private";
jsch.addIdentity(new File(appPublicKey).getAbsolutePath());
com.jcraft.jsch.Session session = jsch.getSession("testusr","localhost", 8002);
session.setConfig("StrictHostKeyChecking", "no");
session.setTimeout(30000);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
String filename = "c:\\temp\\test.tar.gz";
File f = new File(filename);
sftpChannel.put(new FileInputStream(f), "/home/testusr");
例外:
4:
at com.jcraft.jsch.ChannelSftp.getHome(ChannelSftp.java:2403)
at com.jcraft.jsch.ChannelSftp.getCwd(ChannelSftp.java:2412)
at com.jcraft.jsch.ChannelSftp.remoteAbsolutePath(ChannelSftp.java:2904)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:517)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:492)
at com.SftpServiceTest.sendFile(SftpServiceTest.java:183)
at com.SftpServiceTest.main(SftpServiceTest.java:218)
Caused by: java.io.IOException: inputstream is closed
at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2871)
at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2895)
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2315)
at com.jcraft.jsch.ChannelSftp.getHome(ChannelSftp.java:2397)
... 6 more
我似乎找不到任何创建 FileSystemView 的好例子。希望有人可以帮助我,我已经坚持了好几天了!
提前致谢。