使用 JSch 从 SSHD 服务器下载文件:移动端到移动端方案

本文档旨在指导开发者如何使用 JSch 库从运行在移动设备上的 SSHD 服务器下载文件到另一个移动设备。由于移动网络环境的特殊性,直接通过移动网络连接可能会遇到问题,本文将重点介绍在 Wi-Fi 环境下的解决方案,并提供详细的代码示例和注意事项,帮助开发者成功实现文件传输。

解决移动网络连接限制

由于大多数移动运营商会阻止传入连接,因此直接通过移动网络将移动设备用作服务器通常不可行。因此,建议在 Wi-Fi 环境下进行测试和开发。

客户端代码示例 (JSch)

以下代码展示了如何使用 JSch 库连接到 SSHD 服务器并下载文件。

import com.jcraft.jsch.*;

public class Client {

    private static final String USERNAME = "sftptest"; // 替换为你的用户名
    private static final String PASSWORD = "sftptest"; // 替换为你的密码
    private static final String REMOTE_HOST = "192.168.1.100"; // 替换为你的服务器 IP 地址
    private static final int REMOTE_PORT = 8888; // 替换为你的服务器端口
    private static final int SESSION_TIMEOUT = 10000;
    private static final int CHANNEL_TIMEOUT = 5000;

    public void startClient() {
        String localFile = "/sdcard/download/au.au_1"; // 客户端保存文件的路径,确保有写入权限
        String remoteFile = "/data/user/0/com.open.androidsshd/databases/au.au_1"; // 服务端文件路径

        Session jschSession = null;

        try {
            JSch jsch = new JSch();
            //  jsch.setKnownHosts("/home/mkyong/.ssh/known_hosts"); // 可选:如果需要验证主机密钥

            jschSession = jsch.getSession(USERNAME, REMOTE_HOST, REMOTE_PORT);

            // authenticate using private key
            // jsch.addIdentity("/home/mkyong/.ssh/id_rsa");

            // authenticate using password
            jschSession.setPassword(PASSWORD);

            // 忽略主机密钥检查(仅用于测试环境,生产环境不建议)
            java.util.Propertie

s config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); jschSession.setConfig(config); System.out.println("Establishing connection"); // 10 seconds session timeout jschSession.connect(SESSION_TIMEOUT); Channel sftp = jschSession.openChannel("sftp"); // 5 seconds timeout sftp.connect(CHANNEL_TIMEOUT); ChannelSftp channelSftp = (ChannelSftp) sftp; // transfer file from local to remote server //channelSftp.put(localFile, remoteFile); // download file from remote server to local channelSftp.get(remoteFile, localFile); System.out.println("got the file"); channelSftp.exit(); } catch (JSchException | SftpException e) { e.printStackTrace(); } finally { if (jschSession != null) { jschSession.disconnect(); } } } public static void main(String[] args) { Client client = new Client(); client.startClient(); } }

注意事项:

  • 权限: 确保客户端应用具有读取和写入外部存储的权限。在 AndroidManifest.xml 文件中添加以下权限声明:

    
    
    

    同时,在运行时请求这些权限。

  • 依赖: 确保你的项目中包含了 JSch 库。你可以在 build.gradle 文件中添加以下依赖:

    implementation 'com.jcraft:jsch:0.1.55'
  • 主机密钥验证: 在生产环境中,强烈建议启用主机密钥验证,以防止中间人攻击。 你可以使用 jsch.setKnownHosts() 方法设置已知主机文件。

  • IP 地址: 确保客户端和服务器在同一个 Wi-Fi 网络下,并且客户端可以使用服务器的 IP 地址访问它。

  • 路径: 确保客户端和服务端的文件路径正确。

  • 异常处理: 完善异常处理,例如处理连接超时、认证失败等情况。

服务器端代码示例 (Apache SSHD)

以下代码展示了如何在移动设备上运行一个简单的 SSHD 服务器。

import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
import org.apache.sshd.server.command.ScpCommandFactory;
import org.apache.sshd.server.shell.EchoShellFactory;
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.server.command.Command;

import java.io.IOException;
import java.util.Arrays;

public class Server {

    public static void main(String[] args) throws IOException {
        int port = 8888;
        SshServer sshd = SshServer.setUpDefaultServer();
        sshd.setPort(port);
        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser")); // 替换为你的密钥文件
        sshd.setSubsystemFactories(Arrays.>asList(new SftpSubsystemFactory()));
        sshd.setCommandFactory(new ScpCommandFactory());
        sshd.setShellFactory(new EchoShellFactory()); // necessary if you want to type commands over ssh
        sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

            @Override
            public boolean authenticate(String u, String p, ServerSession s) {
                return ("sftptest".equals(u) && "sftptest".equals(p));
            }
        });

        try {
            sshd.start();
            System.out.println("SSHD Server started on port " + port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注意事项:

  • 依赖: 确保你的项目中包含了 Apache SSHD 库。 你可以在 pom.xml (如果使用 Maven) 或 build.gradle (如果使用 Gradle) 文件中添加以下依赖。 请注意,这里使用的是 Maven 格式,Gradle 格式请自行转换。

    
        org.apache.sshd
        sshd-core
        2.9.2  
    
  • 密钥生成: 使用 SimpleGeneratorHostKeyProvider 生成主机密钥。 在实际部署中,应该使用更安全的密钥管理方案。

  • 身份验证: 使用 PasswordAuthenticator 进行密码身份验证。 在实际部署中,应该考虑使用更安全的身份验证方式,例如公钥身份验证。

  • 端口: 确保防火墙没有阻止 SSHD 服务器使用的端口。

  • 线程: 确保在单独的线程中运行 SSHD 服务器,以避免阻塞主线程。

总结

本文介绍了使用 JSch 库从运行在移动设备上的 SSHD 服务器下载文件到另一个移动设备的方法。 由于移动网络的限制,建议在 Wi-Fi 环境下进行测试和开发。 通过提供详细的代码示例和注意事项,希望能够帮助开发者成功实现文件传输功能。 记住,安全性至关重要,在生产环境中,请务必采取适当的安全措施,例如启用主机密钥验证和使用更安全的身份验证方式。