2017-10-20 5 views
0

J'essaye d'envoyer un fichier par SFTP en utilisant JSch de JCraft. Je reçois une exception "org.apache.commons.vfs2.FileSystemException: Impossible de se connecter au serveur SFTP à ...". Il semble que JSch n'a même pas essayé d'envoyer un nom d'utilisateur et un mot de passe. Du côté du serveur OpenSSH (loglevel = DEBUG3) Je vois les:Android + JCraft JSch téléchargement SFTP - "org.apache.commons.vfs2.FileSystemException: Impossible de se connecter au serveur 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 

Il n'y a pas de problème lorsque vous essayez de copier le fichier en utilisant scp de la console. Voici la classe java j'utilise:

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; 
    } 


} 

Et voilà comment j'exécute le téléchargement sftp:

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

... Je viens réussi à se connecter Android studio débogueur au processus de cette application sur le téléphone et j'ai découvert qu'il y a quelques bibliothèques manquantes. Alors, je l'ai ajouter:

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

Je compilait le APK après chaque lib ajouté et il y a un problème avec le dernier:

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 

Tout savoir tous les libs de dépendance je devrai installer pour exécuter le code?

Vive

Répondre

0

Après un certain temps, j'ai commencé depuis le début et utilisé différentes solutions que je recommande car il a travaillé hors de la boîte. J'ai utilisé Zehon qui est basé sur JCraft JSch aussi. Vous pouvez l'obtenir à partir de:

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

L'exemple de code pour utiliser le SFTP fonctionne très bien. Vous pouvez l'obtenir ici:

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

En outre, du côté serveur, serveur OpenSSH a besoin d'avoir ces lignes dans le fichier de configuration:

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

Bonne codage! :)