我尝试使用JCraft的JSch通过SFTP发送文件。我收到异常" org.apache.commons.vfs2.FileSystemException:无法连接到..."的SFTP服务器。似乎JSch甚至没有尝试发送用户名和密码。从OpenSSH服务器端(loglevel = DEBUG3)我看到了这些:
var path = @"path/file.xlsx";
var file = new FileInfo(path);
var package = new ExcelPackage(file, "password");
尝试从控制台使用scp复制文件时没有问题。 这是我使用的java类:
Oct 19 22:20:30 android sshd[10973]: debug3: oom_adjust_restore
Oct 19 22:20:30 android sshd[10973]: debug1: Set /proc/selfoom_score_adj to 0
Oct 19 22:20:30 android sshd[10973]: debug1: rexec start in 4 out 4 newsock 4 pipe 6 sock 7
Oct 19 22:20:30 android sshd[10973]: debug1: inetd sockets after dupping: 3, 3
Oct 19 22:20:30 android sshd[10973]: Connection from 192.168.0.165 port 45653 on 192.168.0.100 port 22
Oct 19 22:20:30 android sshd[10973]: Did not receive identification string from 192.168.0.165 port 45653
这就是我执行sftp上传的方式:
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
import java.io.File;
public class SftpExample {
public static void main(String[] args) {
String hostName = "192.168.0.100";
String username = "admin";
String password = "admin";
String localFilePath = "/storage/emulated/0/file.txt";
String remoteFilePath = "file.txt";
upload(hostName, username, password, localFilePath, remoteFilePath);
exist(hostName, username, password, remoteFilePath);
download(hostName, username, password, localFilePath, remoteFilePath);
delete(hostName, username, password, remoteFilePath);
}
// Method to upload a file in Remote server
public static void upload(String hostName, String username,
String password, String localFilePath, String remoteFilePath) {
File file = new File(localFilePath);
if (!file.exists())
throw new RuntimeException("Error. Local file not found");
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
// Create local file object
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
// Create remote file object
FileObject remoteFile = manager.resolveFile(
createConnectionString(hostName, username, password,
remoteFilePath), createDefaultOptions());
// Copy local file to sftp server
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
System.out.println("File upload success");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
}
// Download file function:
public static void download(String hostName, String username,
String password, String localFilePath, String remoteFilePath) {
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
String downloadFilePath = localFilePath.substring(0,
localFilePath.lastIndexOf("."))
+ "_downlaod_from_sftp"
+ localFilePath.substring(localFilePath.lastIndexOf("."),
localFilePath.length());
// Create local file object
FileObject localFile = manager.resolveFile(downloadFilePath);
// Create remote file object
FileObject remoteFile = manager.resolveFile(
createConnectionString(hostName, username, password,
remoteFilePath), createDefaultOptions());
// Copy local file to sftp server
localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
System.out.println("File download success");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
}
// Delete file in remote system:
public static void delete(String hostName, String username,
String password, String remoteFilePath) {
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
// Create remote object
FileObject remoteFile = manager.resolveFile(
createConnectionString(hostName, username, password,
remoteFilePath), createDefaultOptions());
if (remoteFile.exists()) {
remoteFile.delete();
System.out.println("Delete remote file success");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
}
// Check remote file is exist function:
public static boolean exist(String hostName, String username,
String password, String remoteFilePath) {
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
// Create remote object
FileObject remoteFile = manager.resolveFile(
createConnectionString(hostName, username, password,
remoteFilePath), createDefaultOptions());
System.out.println("File exist: " + remoteFile.exists());
return remoteFile.exists();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
}
// Establishing connection
public static String createConnectionString(String hostName,
String username, String password, String remoteFilePath) {
return "sftp://" + username + ":" + password + "@" + hostName + "/" + remoteFilePath;
}
// Method to setup default SFTP config:
public static FileSystemOptions createDefaultOptions()
throws FileSystemException {
// Create SFTP options
FileSystemOptions opts = new FileSystemOptions();
// SSH Key checking
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
// Root directory set to user home
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
// Timeout is count by Milliseconds
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
return opts;
}
}
...我刚刚设法将Android Studio调试器连接到手机上该应用的流程,并且我发现有一些缺少的库。所以,我添加了:
SftpExample.upload(hostName,username,password,localFilePath,remoteFilePath);
我在添加每个lib后编译APK,最后一个有问题:
org.apache.commons.net.ftp
org.apache.commons.httpclient
org.apache.jackrabbit.webdav.client
任何人都知道我需要安装所有依赖库来运行代码吗?
干杯
答案 0 :(得分:0)
过了一段时间,我从一开始就开始使用不同的解决方案,我应该推荐它,因为它开箱即用。我也使用了基于JCraft JSch的Zehon。你可以从:
获得它http://www.zehon.com/downloads.htm
使用SFTP的示例代码效果很好。你可以从这里得到它:
http://www.zehon.com/SFTP_samples.htm
此外,从服务器端,OpenSSH服务器需要在配置文件中包含这些行:
KexAlgorithms +diffie-hellman-group1-sha1
KexAlgorithms +diffie-hellman-group-exchange-sha1
Ciphers aes128-cbc,3des-cbc,blowfish-cbc
快乐的编码! :)