java – Apache Mina和JSch无法设置home dir
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Apache Mina和JSch无法设置home dir,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5160字,纯文字阅读大概需要8分钟。
内容图文
![java – Apache Mina和JSch无法设置home dir](/upload/InfoBanner/zyjiaocheng/771/17226f8d339e47c0b6ef6e7b471e8fed.jpg)
我正在尝试使用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();
JSch代码:
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的任何好例子.希望有人可以帮助我,我已经坚持了好几天!
提前致谢.
解决方法:
这就是我最终设置Jsch& Mina在单元测试中:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {“classpath:context.xml”})
公共类SftpTaskletLetterTest扩展AbstractAomTest {
@Autowired
private SftpTaskletLetter cut;
private SshServer sshd = null;
@Before
public void setUp() throws Exception {
sshd = createAndStartSSHServer();
}
@After
public void tearDown() throws Exception {
sshd.stop();
}
@Test
public void testPutAndGetFile() throws Exception {
JSch jsch = new JSch();
Hashtable<String, String> config = new Hashtable<>();
JSch.setConfig(config);
Session session = jsch.getSession("assentisftp", "127.0.0.1", 22);
UserInfo ui = new MyUserInfo();
session.setUserInfo(ui);
session.connect();
// Channel channel = session.openChannel("session"); // works
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
final String testFileContents = "some file contents";
String uploadedFileName = "uploadFile";
sftpChannel.put(new ByteArrayInputStream(testFileContents.getBytes()), uploadedFileName);
String downloadedFileName = "downLoadFile";
sftpChannel.get(uploadedFileName, downloadedFileName);
File downloadedFile = new File(downloadedFileName);
assertTrue(downloadedFile.exists());
if (sftpChannel.isConnected()) {
sftpChannel.exit();
}
if (session.isConnected()) {
session.disconnect();
}
}
public static SshServer createAndStartSSHServer() throws IOException {
SshServer result = SshServer.setUpDefaultServer();
result.setPort(22);
result.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
result.setPasswordAuthenticator(new PasswordAuthenticator() {
public boolean authenticate(String username, String password, ServerSession session) {
return "assentisftp".equals(username);
}
});
result.setSubsystemFactories(Arrays.<NamedFactory<Command>> asList(new SftpSubsystem.Factory()));
result.start();
return result;
}
@Override
protected MandatorType getMandator() {
return MandatorType.MAN;
}
public static class MyUserInfo implements UserInfo {
@Override
public String getPassphrase() {
return "";
}
@Override
public String getPassword() {
return "";
}
@Override
public boolean promptPassword(String message) {
return true;
}
@Override
public boolean promptPassphrase(String message) {
return true;
}
@Override
public boolean promptYesNo(String message) {
return true;
}
@Override
public void showMessage(String message) {
}
}
}
内容总结
以上是互联网集市为您收集整理的java – Apache Mina和JSch无法设置home dir全部内容,希望文章能够帮你解决java – Apache Mina和JSch无法设置home dir所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。