如何在Java中设置Apache Mina Sshd服务器的根目录
我使用Apache Mina Sshd API在java中启动本地SFTP服务器。在SFTP客户端中,我使用Jcraft jsch API创建我的SFTP客户端。我成功启动了一台服务器。问题是,我想编写一些单元测试用例,检查客户端是否可以将一些文件放入服务器的根目录中。目前我的SFTP服务器没有任何根目录。所以我想知道是否有任何方法可以设置服务器的根目录 例如:C:\sftp如何将此路径设置为我的服务器根目录。这样客户端每次连接到服务器时都可以读取和写入文件。谢谢如何在Java中设置Apache Mina Sshd服务器的根目录,java,sftp,jsch,apache-mina,Java,Sftp,Jsch,Apache Mina,我使用Apache Mina Sshd API在java中启动本地SFTP服务器。在SFTP客户端中,我使用Jcraft jsch API创建我的SFTP客户端。我成功启动了一台服务器。问题是,我想编写一些单元测试用例,检查客户端是否可以将一些文件放入服务器的根目录中。目前我的SFTP服务器没有任何根目录。所以我想知道是否有任何方法可以设置服务器的根目录 例如:C:\sftp如何将此路径设置为我的服务器根目录。这样客户端每次连接到服务器时都可以读取和写入文件。谢谢 public class Sf
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");
}
}
}
公共类SftpServerStarter{
专用ssh服务器sshd;
专用最终静态记录器=
getLogger(SftpServerStarter.class);
公开作废开始(){
sshd=SshServer.setUpDefaultServer();
设置端口(22);
sshd.setHost(“localhost”);
setPasswordAuthenticator(新的MyPasswordAuthenticator());
setPublickeyAuthenticator(新的MyPublickeyAuthenticator());
setKeyPairProvider(新的SimpleGeneratorHostKeyProvider());
sshd.setSubsystemFactories(
Arrays.asList(新的sftpusbsystem.Factory());
setCommandFactory(新的ScpCommandFactory());
试一试{
logger.info(“启动…”);
sshd.start();
logger.info(“已启动”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
logger.info(“无法启动服务器”);
}
}
}
默认情况下,它从名为user.dir的系统属性获取根路径
为了改变这一点,您可以覆盖NativeFileSystemView
中的getVirtualUserDir()
,并返回您的路径
sshd.setFileSystemFactory(new NativeFileSystemFactory() {
@Override
public FileSystemView createFileSystemView(final Session session) {
return new NativeFileSystemView(session.getUsername(), false) {
@Override
public String getVirtualUserDir() {
return "C:\\MyRoot";
}
};
};
});
您还可以通过以下链接了解如何在ApacheMina sshd SFTP服务器中使用不同的sshd core版本设置根目录
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>0.10.0</version>
</dependency>
org.apache.sshd
在较新的sshd版本中,您可以使用org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory
,并通过方法setFileSystemFactory
将其提供给SshServer
实例
片段:
VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory();
fileSystemFactory.setDefaultHomeDir("home.directory");
sshd.setFileSystemFactory(fileSystemFactory)
可能是@DanielNewtown的复制品嗨,伙计,我知道这是很久以前的事了。。你能在链接中重新发布那篇文章吗?我们说话的时候,它现在返回404。提前谢谢,非常感谢!这是0.14.0版本的sshd-core.Hi Tharaka唯一可用的解决方案,我现在正在处理相同类型的用例,但我想在用户使用winscp或任何其他sftp客户端登录时为其创建一个完全虚拟的文件系统。我正在尝试为用户创建或设置根目录,例如“/xyz”和子目录,例如“/abc”,而不是任何系统文件目录(例如:“C:\”)。知道如何实现吗?这现在需要一个Path对象,例如:fileSystemFactory.setDefaultHomeDir(Files.createTempDirectory(“sshd”)
VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory();
fileSystemFactory.setDefaultHomeDir("home.directory");
sshd.setFileSystemFactory(fileSystemFactory)