如何在Java中使用Apache Mina Sshd Server设置根目录 [英] How to Set Root Directory in Apache Mina Sshd Server in Java

查看:275
本文介绍了如何在Java中使用Apache Mina Sshd Server设置根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Apache Mina Sshd API 在java中启动本地SFTP服务器。在SFTP客户端我使用 Jcraft jsch API 创建我的SFTP客户端。我成功启动服务器。问题是我想写一些单元测试用例来检查客户端是否可以将一些文件放入服务器的根目录。目前我的SFTP服务器没有任何根目录。所以我想知道是否有任何设置服务器根目录的方法。

I use Apache Mina Sshd API to start up a local SFTP server in java.In SFTP client i use Jcraft jsch API to create my SFTP client.I successfully start up a server.The problem is that i want to write some unit test cases to check whether client can put some files into server's root directory. Currently my SFTP server doesn't have any root directory.So i would like to know that is there is any approach to set server's root directory.

例如: C:\ sftp 如何将此路径设置为我的服务器根目录。然后,客户端每次与服务器连接时都可以读取和写入文件。谢谢。

Eg: C:\sftp How can i set this path as my server root directory.so then client can read and write files to it every time connect with the server.Thank you.

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");
		}
	}
	
}

推荐答案

在默认情况下,它从系统属性中获取根路径,名为 user.dir

In Default it takes the root path from System property called user.dir

为了更改此设置,您可以覆盖 getVirtualUserDir() NativeFileSystemView 中并返回您的路径。

Inorder to change this, you can override getVirtualUserDir() in NativeFileSystemView and return your path.

    sshd.setFileSystemFactory(new NativeFileSystemFactory() {
        @Override
        public FileSystemView createFileSystemView(final Session session) {
            return new NativeFileSystemView(session.getUsername(), false) {
                @Override
                public String getVirtualUserDir() {
                    return  "C:\\MyRoot";
                }
            };
        };
    });

这篇关于如何在Java中使用Apache Mina Sshd Server设置根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆