Use Apache Mina SSHD :: SFTP to achieve safe file transmission
Use Apache Mina SSHD :: SFTP to achieve safe file transmission
Apache Mina is a scalable Java network application framework, and the SSHD (SSH server side) is one of the sub -items. It provides APIs for building a secure SSH server and client.In this article, we will explore how to use the SFTP function of Apache Mina SSHD to achieve secure file transmission.
Before starting, make sure you have the correct installation of Apache Mina SSHD and the corresponding dependencies.
Step 1: Create a SSH server
First, we need to create a SSH server to provide safe file transmission services.The following is a simple example code:
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class SftpServer {
public static void main(String[] args) throws IOException {
// Create an SSH server instance
SshServer sshd = SshServer.setUpDefaultServer();
// Set the port number
sshd.setPort(22);
// Set the host key of the SSH server
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("hostkey.ser")));
// Set the SFTP subsystem factory
sshd.setSubsystemFactories(createSubsystemFactories());
// Set the root directory of the virtual file system
sshd.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get("/sftp")));
// Start the SSH server
sshd.start();
System.out.println("SFTP Server started on port " + sshd.getPort());
}
private static List<NamedFactory<?>> createSubsystemFactories() {
List<NamedFactory<?>> subsystemFactories = new ArrayList<>();
subsystemFactories.add(new SftpSubsystemFactory());
return subsystemFactories;
}
}
In the above code, we first create an instance of a `sshServer` and set the port number and host key of the SSH server.Next, we set the SFTP subsystem factory through the method of `setsubsystemFactories ()`, and set the root directory of the virtual file system through the method of `setFilesystemFactory ()`.Finally, call the `Start ()` method to start the SSH server.
Step 2: Create the SFTP client
Create a SFTP client to upload and download files.The following is a simple example code:
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.channel.ChannelSftp;
import org.apache.sshd.client.session.ClientSession;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class SftpClient {
public static void main(String[] args) throws IOException {
// Create SSH client instance
SshClient sshClient = SshClient.setUpDefaultClient();
// Connect to the server
sshClient.start();
ClientSession session = sshClient.connect("localhost", 22).await().getSession();
session.authPassword("username", "password").await();
// Open the SFTP channel
ChannelSftp channel = session.createSftpChannel();
channel.connect();
// upload files
Path localFile = Paths.get("local-file.txt");
String remoteDir = "/upload";
channel.put(localFile.toString(), remoteDir + "/" + localFile.getFileName());
// download file
String remoteFile = "/download/remote-file.txt";
Path localDir = Paths.get("download");
channel.get(remoteFile, localDir.resolve(Paths.get(remoteFile).getFileName()).toString());
// Turn off the SFTP channel and SSH session
channel.disconnect();
session.disconnect();
sshClient.stop();
}
}
In the above code, we first created an instance of the `sshclient` and connected to the SSH server through the` Connect () "method.Then, we use the `Authpassword () method to verify the identity.Next, we created a SFTP channel through the method of `CreateSFTPChannel ()` and connected to the server through the `Connect ()" method.Then, we can upload local files with the `put ()" method, or use the `Get ()` method to download the remote file.Finally, we need to turn off the SFTP channel and SSH session through the method of `Disconnect ()`.
Through the above steps, we successfully use the SFTP function of Apache Mina SSHD to achieve safe file transmission.You can perform more customized operations according to your needs, such as monitoring or abnormal treatment of uploading and downloading progress.I hope this article will help you understand the use of Apache Mina SSHD :: SFTP to achieve safe file transmission.