如何在Java中使用Apache Mina Sshd Server设置根目录

我使用Apache Mina Sshd APIjava中启动本地SFTP服务器。在SFTP客户端我使用Jcraft jsch API创建我的SFTP客户端。我成功启动了服务器。问题是我想写一些unit testing用例到检查客户端是否可以将某些文件放入服务器的根目录。 目前我的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>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"); } } } 

在Default中,它从System属性中获取名为user.dir的根路径

为了更改它,您可以覆盖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"; } }; }; }); 

您还可以按照以下链接了解如何在具有不同sshd-core版本的Apache Mina sshd SFTP服务器中设置根目录。

  org.apache.sshd sshd-core 0.10.0  

  org.apache.sshd sshd-core 0.14.0  

如何在Apache Mina sshd-core版本0.14.0中覆盖getVirtualUserDir()