简体   繁体   English

Android + JCraft JSch SFTP上传-“ org.apache.commons.vfs2.FileSystemException:无法在以下位置连接到SFTP服务器”

[英]Android + JCraft JSch SFTP upload - “org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at…”

I'm trying to send a file by SFTP using JSch from JCraft. 我正在尝试使用JCraft的JSch通过SFTP发送文件。 I'm getting an exception "org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at...". 我收到异常“ org.apache.commons.vfs2.FileSystemException:无法在...连接到SFTP服务器”。 It seems like JSch isn't even tried to send username and password. 看来JSch甚至没有尝试发送用户名和密码。 From the OpenSSH server side (loglevel=DEBUG3) i'm seeing those : 从OpenSSH服务器端(loglevel = DEBUG3),我看到了那些:

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

There is no problem when trying to copy file using scp from the console. 尝试从控制台使用scp复制文件时没有问题。 Here is the java class I'm using : 这是我正在使用的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;
    }


}

And this is how I execute sftp upload : 这就是我执行sftp upload的方式:

SftpExample.upload(hostName,username,password,localFilePath,remoteFilePath);

...I've just managed to connect Android Studio debugger to the process of that app on the phone and I've find out that there are some missing libs. ...我已经设法将Android Studio调试器连接到手机上的该应用程序的进程,并且发现其中缺少一些库。 So, I've add : 因此,我添加:

org.apache.commons.net.ftp
org.apache.commons.httpclient
org.apache.jackrabbit.webdav.client

I was compiling the APK after each lib added and there is a problem with the last one : 添加每个库后,我正在编译APK,最后一个库存在问题:

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

Any one know all dependency libs I will need to install to run the code? 有人知道我需要安装所有依赖库才能运行代码吗?

Cheers 干杯

After some time, I've started from the beginning and used different solution which I should recommend as it worked out of the box. 一段时间后,我从头开始,并使用了不同的解决方案,当它开箱即用时,我应该推荐它。 I've used Zehon which is based on JCraft JSch too. 我也使用了基于JCraft JSch的Zehon。 You can get it from : 您可以从:

http://www.zehon.com/downloads.htm http://www.zehon.com/downloads.htm

The sample code for using the SFTP works great. 使用SFTP的示例代码效果很好。 You can get it from here : 您可以从这里获取:

http://www.zehon.com/SFTP_samples.htm http://www.zehon.com/SFTP_samples.htm

Also, from the server side, OpenSSH server needs to have those lines in the config file : 另外,在服务器端,OpenSSH服务器需要在配置文件中包含以下几行:

KexAlgorithms +diffie-hellman-group1-sha1
KexAlgorithms +diffie-hellman-group-exchange-sha1
Ciphers aes128-cbc,3des-cbc,blowfish-cbc

Happy coding! 编码愉快! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.apache.commons.vfs2.FileSystemException:无法连接到 SFTP 服务器 - org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server 当使用 Apache Commons VFS 进行 SFTP 上传时,必须面对 org.apache.commons.vfs2.FileSystemException:找不到带有 URI 的文件 - When SFTP Upload using Apache Commons VFS have to face org.apache.commons.vfs2.FileSystemException: Could not find file with URI org.apache.commons.vfs.FileSystemException:无法在端口 21 上的“sftp://username:***@114.XX.XX.XX/”连接到 SFTP 服务器 - org.apache.commons.vfs.FileSystemException: Could not connect to SFTP server at "sftp://username:***@114.XX.XX.XX/" on port 21 无法使用jcraft JSch连接到SFTP - Not able to connect to SFTP using jcraft JSch SFTP上传下载使用Apache Commons VFS进行存在和移动 - SFTP Upload Download Exist and Move using Apache Commons VFS android中jcraft jsch SFTP库的ClassNotFound异常 - ClassNotFound Exception for jcraft jsch SFTP library in android 使用 Apache Commons VFS-SFTP,上传到服务器 - Using Apache Commons VFS- SFTP, uploading to a server Apache VFS2 - 无法将文件上传到 SFTP 服务器 - Apache VFS2 - can't upload file to SFTP server java.lang.NoClassDefFoundError:org / apache / commons / vfs / FileSystemException - java.lang.NoClassDefFoundError: org/apache/commons/vfs/FileSystemException 文件上传到SFTP失败(Apache VFS) - File upload to SFTP fails (Apache VFS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM