Android + JCraft JSch SFTP上传 - “org.apache.commons.vfs2.FileSystemException:无法连接到SFTP服务器...”
问题描述:
我试图通过SFTP使用JSraft从SFraft发送文件。我收到一个异常“org.apache.commons.vfs2.FileSystemException:无法连接到SFTP服务器......”。好像JSch甚至没有试图发送用户名和密码。从OpenSSH服务器端(日志等级= DEBUG3)我看到那些:Android + JCraft JSch SFTP上传 - “org.apache.commons.vfs2.FileSystemException:无法连接到SFTP服务器...”
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
有尝试使用scp从控制台复制文件时没有问题。 这里是我使用的Java类:
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;
}
}
而且我这是怎么执行SFTP上传:
SftpExample.upload(hostName,username,password,localFilePath,remoteFilePath);
......我好不容易到Android Studio调试器连接到进程在手机上的应用程序,我发现有一些缺失的库。所以,我补充一下:
org.apache.commons.net.ftp
org.apache.commons.httpclient
org.apache.jackrabbit.webdav.client
我编译APK添加的每个LIB后有一个与最后一个问题:
Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE
File1: /home/g/StudioProjects/AndroidProject/app/build/libs/jackrabbit-webdav-2.2.5.jar
File2: /home/g/.gradle/caches/modules-2/files-2.1/org.objenesis/objenesis/2.6/639033469776fd37c08358c6b92a4761feb2af4b/objenesis-2.6.jar
任何一个知道的所有依赖库我需要安装运行代码?
干杯
答
一段时间后,我从一开始就开始,使用了不同的解决方案,我应该建议,因为它的工作开箱。我也使用了基于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
编码愉快! :)