2017-10-20 3 views
0

JCraftのJSchを使用してSFTPでファイルを送信しようとしています。 "org.apache.commons.vfs2.FileSystemException:SFTPサーバーに接続できませんでした..."という例外が発生しています。 JSchはユーザー名とパスワードを送信しようとしていないようです。私はそれらを見ている(ログレベル= DEBUG3)OpenSSHサーバー側から: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); 

を...私はちょうどプロセスにアンドロイドStudioデバッガを接続することができましたそのアプリの電話に私はいくつかの不足しているライブラリがあることを知っている。だから、私は追加しました:

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

をそれぞれのlibを追加した後、私はAPKをコンパイルして、最後の1に問題がある:

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 

いずれは私が必要とするすべての依存LIBSを知っていますコードを実行するためにインストールしますか?

乾杯

答えて

0

いくつかの時間後、私は最初から開始し、それが箱から出して働いていたとして、私はお勧めしなければならない別のソリューションを使用しました。私はJCraft JSchをベースにしたZehonを使用しました。 SFTPを使用するためのサンプルコードは素晴らしい作品

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

:あなたはからそれを得ることができます。あなたはここからそれを得ることができます。

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 

ハッピーコーディングを! :)

関連する問題