我使用 Apache Mina Sshd API 在 java 中启动本地 SFTP 服务器。在 SFTP 客户端中,我使用 Jcraft jsch API 创建我的 SFTP 客户端。我成功启动建立服务器。问题是我想编写一些单元测试用例来检查客户端是否可以将一些文件放入服务器的根目录中。目前我的SFTP服务器没有任何根目录。所以我想知道有没有什么方法可以设置服务器的根目录。
例如:C:\sftp 我如何将此路径设置为我的服务器根目录。这样客户端每次连接到服务器时都可以读取和写入文件。谢谢。
public class SftpServerStarter {
private SshServer sshd;
private final static Logger logger =
LoggerFactory.getLogger(SftpServerStarter.class);
public void start(){
sshd = SshServer.setUpDefaultServer();
sshd.setPort(22);
sshd.setHost("localhost");
sshd.setPasswordAuthenticator(new MyPasswordAuthenticator());
sshd.setPublickeyAuthenticator(new MyPublickeyAuthenticator());
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
sshd.setSubsystemFactories(
Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
sshd.setCommandFactory(new ScpCommandFactory());
try {
logger.info("Starting ...");
sshd.start();
logger.info("Started");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.info("Can not Start Server");
}
}
}
最佳答案
在最近的 sshd 版本中,您可以使用 org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory
并通过方法 将其提供给
。SshServer
实例设置文件系统工厂
片段:
VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory();
fileSystemFactory.setDefaultHomeDir("home.directory");
sshd.setFileSystemFactory(fileSystemFactory)
关于java - 如何在 Java 中设置 Apache Mina Sshd 服务器的根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389249/