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.
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");
}
}
}
The Apache MINA SSHDlibrary is a great way to provide SSHD client or server functionality in your application. Unfortunately, it is not especially well documented and recent refactoring in the project has left most of the few usage examples you can find obsolete. This repoprovides a very simple server example to help you get started.
In this tutorial, we'll show how to establish a connection to a remote SSH server with Java using the JSch and Apache MINA SSHD libraries. In our examples, we'll first open the SSH connection, then execute one command, read the output and write it to the console, and, finally, close the SSH connection.
To change the document root for your Apache web server simply open the corresponding file with your favourite text editor and search for DocumentRoot. # # DocumentRoot: The directory out of which you will serve your # documents.
Apache Mina SSHD has a lot of available Maven dependencies (for example FTP, SCP, etc.), but since we're using only core functionalities, we'll only add core Maven dependency to our project:
In more recent sshd versions you can use org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory
and supply it to the SshServer
instance via method setFileSystemFactory
.
Snippet:
VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory();
fileSystemFactory.setDefaultHomeDir("home.directory");
sshd.setFileSystemFactory(fileSystemFactory)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With