2017-10-20 271 views
0

我試圖通過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 

任何一個知道的所有依賴庫我需要安裝運行代碼?

乾杯

回答

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 

編碼愉快! :)