【发布时间】:2019-10-06 23:32:58
【问题描述】:
我需要通过 SSH 从 RaspberryPi Model 3b+ 下载一个 txt 文件,并希望在 android 设备上存储另一个文件。问题是文件下载失败。
IDE:Android Studio | SSH 库:JSch
我尝试了很多不同的代码示例,几乎所有的代码都是一样的。但他们也没有工作。 其他人的解决方案正在处理异步任务。如果这是我的问题的解决方案,有人可以为异步部分编写示例代码吗?我真的不知道如何处理它。
这是我的代码:
public static String username = "BIOGAS";
public static String host = "192.168.1.29";
public static String password = "clientOG58";
public static String targetFile = "/home/BIOGAS/ies/data.txt";
public static int port = 22;
public String download() throws Exception {
JSch jsch = new JSch();
Session session = null;
String r = "No Data";
try {
session = jsch.getSession(username, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
r = "" + sftpChannel.get(targetFile).toString();
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
return r;
}
我希望文件将作为字符串下载,并且该字符串将作为返回语句返回。
【问题讨论】: