
本文旨在帮助开发者解决在使用 JSch 客户端从运行在移动设备上的 SSHD 服务器下载文件时遇到的 java.net.SocketException: Connection reset 错误。我们将分析错误原因,并提供基于 Wi-Fi 连接的解决方案,包括客户端和服务端配置,以及必要的权限声明。由于移动运营商通常会阻止传入连接,因此本文主要针对 Wi-Fi 环境下的文件传输。
java.net.SocketException: Connection reset 错误通常表示连接在建立后被意外中断。这可能由多种原因引起,包括:
由于移动运营商通常会阻止传入连接,直接从移动网络访问 SSHD 服务器往往不可行。因此,建议在 Wi-Fi 环境下进行测试。
以下代码展示了如何使用 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"); //如果需要验证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);
// 忽略 Host Key Checking,生产环境不建议
java.util.Properties 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) {
new Client().startClient();
}
}注意事项:
依赖项:
确保在客户端项目的 build.gradle 文件中添加了以下依赖项:
implementation files('libs\jsch-0.1.55.jar')
implementation group: 'org.apache.sshd', name: 'sshd-core', version: '0.6.0'
testImplementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.2'
implementation group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'以下代码展示了如何在移动设备上配置 SSHD 服务器。
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.server.shell.EchoShellFactory;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystem;
import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.command.CommandFactory;
import org.apache.sshd.server.command.ScpCommandFactory;
import java.io.IOException;
import java.util.Arrays;
import java.nio.file.Paths;
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(Paths.get("hostkey.ser")));
sshd.setSubsystemFactories(Arrays.<NamedFactory<org.apache.sshd.common.subsystem.Subsystem>>asList(new SftpSubsystem.Factory()));
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));
}
});
// Virtual file system
VirtualFileSystemFactory vfss = new VirtualFileSystemFactory(Paths.get("/"));
sshd.setFileSystemFactory(vfss);
try {
sshd.start();
System.out.println("SSHD Server started on port " + port);
} catch (IOException e) {
e.printStackTrace();
}
}
}注意事项:
在 Android 应用的 AndroidManifest.xml 文件中,添加以下权限声明:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
解释:
通过以上步骤,你应该能够成功地使用 JSch 客户端从运行在移动设备上的 SSHD 服务器下载文件。请确保在 Wi-Fi 环境下进行测试,并根据实际情况调整客户端和服务端配置。如果仍然遇到问题,请仔细检查网络连接、防火墙设置和权限声明。记住,移动运营商通常会阻止传入连接,因此直接从移动网络访问 SSHD 服务器可能不可行。
以上就是使用 JSch 从 SSHD 服务器下载文件:故障排除及解决方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号