2016-05-12 63 views
0

ローカルフォルダからリモートフォルダにファイルをアップロードしようとしていますが、エラーが発生しています。私は誰かが解決策を見つけるのを助けることを望む。ありがとう。SFTPを使用してローカルフォルダからリモートフォルダにファイルをアップロード

コードは以下の通り:私は、コードを実行するたびに

public static void main(String[] args) { 
     String server = "www.xyz.com"; 
     int port = 22; 
     String user = "user"; 
     String pass = "[email protected]"; 

     FTPClient ftpClient = new FTPClient(); 
     try { 

      ftpClient.connect(server, port); 
      System.out.println("1"); 
      ftpClient.login(user, pass); 
      System.out.println("2"); 
      ftpClient.enterLocalPassiveMode(); 

      System.out.println("3"); 

      ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
      System.out.println("4"); 

      // APPROACH #1: uploads first file using an InputStream 
      File firstLocalFile = new File("D:/UploadServlet.java"); 

      String firstRemoteFile = "Projects.zip"; 
      InputStream inputStream = new FileInputStream(firstLocalFile); 

      System.out.println("Start uploading first file"); 
      boolean done = ftpClient.storeFile(firstRemoteFile, inputStream); 
      inputStream.close(); 
      if (done) { 
       System.out.println("The first file is uploaded successfully."); 
      } 



     } catch (IOException ex) { 
      System.out.println("Error: " + ex.getMessage()); 
      ex.printStackTrace(); 
     } finally { 
      try { 
       if (ftpClient.isConnected()) { 
        ftpClient.logout(); 
        ftpClient.disconnect(); 
       } 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } 

は以下のエラーを取得しています。

Error: Could not parse response code. 
Server Reply: SSH-2.0-OpenSSH_4.3 
org.apache.commons.net.MalformedServerReplyException: Could not parse response code. 
Server Reply: SSH-2.0-OpenSSH_4.3 
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:284) 
    at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:335) 
    at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:550) 
    at org.apache.commons.net.SocketClient.connect(SocketClient.java:163) 
    at FTPUploadFileDemo.main(FTPUploadFileDemo.java:26) 
org.apache.commons.net.MalformedServerReplyException: Could not parse response code. 
Server Reply: Protocol mismatch. 
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:284) 
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:460) 
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:520) 
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:569) 
    at org.apache.commons.net.ftp.FTP.quit(FTP.java:781) 
    at org.apache.commons.net.ftp.FTPClient.logout(FTPClient.java:706) 
    at FTPUploadFileDemo.main(FTPUploadFileDemo.java:58) 
+0

http://stackoverflow.com/questions/15621325/ftpsclient-returns-malformedserverreplyexception-cannot-parse-response-codeとhttp://stackoverflow.com/questions/14617/java-what-is-最善の方法 - サーバーからファイルへの変換 – gonephishing

+0

ポートが間違っていますか? '22 'はSSHのデフォルトです。ポート21でデフォルトのFTPを使用しています。 SFTP(FTP over SSH)またはFTPS(FTP over SSL)を使用しますか? – SubOptimal

+0

@SubOptimal SSHでFTPをアップロードしようとしています –

答えて

0

有効なftpサーバーとポートを使用していますか?あなたの例のリターン "SSH-2.0-OpenSSH_5.9p1のDebian-5ubuntu1" それはFTPSを(サポートしているとして、あなたは、ライブラリApache Commons Netを使用することはできませんSFTP(SSH経由でFTP)のために

telnet www.xyz.com 22 
SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1 
+0

はい。 www.xyz.comはちょうど例とその正しいolyです –

+0

私はFTPClientがSSH、より多くの詳細とアドバイスでFTPで動作しないと思います重複コメント:http://stackoverflow.com/questions/15621325/ftpsclient-returns- malformedserverreplyexception-can not-parse-response-code –

+0

http://kodehelp.com/java-program-for-uploading-file-to-sftp-server/ - もう1つの便利なリンク –

0

から

String server = "www.xyz.com"; 
int port = 22; 

サーバーFTP over SSL)。

しかし、たとえば、Jschという純粋なJavaのSSH2実装(BSDスタイルライセンス)または商用SFTPライブラリを使用できます。

JschのSFTPの例は、http://www.jcraft.com/jsch/examples/Sftp.javaにあります。

0

以下のSFTPユーティリティは、アップロード、存在、ダウンロード、移動、削除操作の例を示しています。私はこれをいくつかのプロジェクトで使ってきました。以下のコードは、Apache Commons VFSライブラリからのapi呼び出しのみを使用しています。

あなたが置くことを確認してください、あなたのプロジェクトのビルド・パスに以下のJARの:

  1. コモンズ・ログ-1.1.3.jar
  2. コモンズ-vfs2-2.0.jarが
  3. hamcrestコア
  4. JSCH-0.1.50.jar

-1.3.jar互換性のある他のバージョンも同様に行うだろう。

import java.io.File; 

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; 

/** 
* The class SFTPUtil containing uploading, downloading, checking if file exists 
* and deleting functionality using Apache Commons VFS (Virtual File System) 
* Library 
* 
* @author Ashok 
* 
*/ 
public class SFTPUtility { 

    public static void main(String[] args) { 
     String hostName = "PutYourHostNameHere"; 
     String username = "PutYourUserNameForHostHere"; 
     String password = "PutYourPasswordForHostHere"; 

     String localFilePath = "C:\\FakePath\\FakeFile.txt"; 
     String remoteFilePath = "/FakeRemotePath/FakeRemoteFile.txt";  
     String remoteTempFilePath = "/FakeRemoteTempPath/FakeRemoteTempFile.txt"; 

     upload(hostName, username, password, localFilePath, remoteFilePath); 
     exist(hostName, username, password, remoteFilePath); 
     download(hostName, username, password, localFilePath,remoteFilePath); 
     move(hostName, username, password, remoteFilePath, remoteTempFilePath); 
     delete(hostName, username, password, remoteFilePath); 
    } 

    /** 
    * Method to upload a file in Remote server 
    * 
    * @param hostName 
    *   HostName of the server 
    * @param username 
    *   UserName to login 
    * @param password 
    *   Password to login 
    * @param localFilePath 
    *   LocalFilePath. Should contain the entire local file path - 
    *   Directory and Filename with \\ as separator 
    * @param remoteFilePath 
    *   remoteFilePath. Should contain the entire remote file path - 
    *   Directory and Filename with/as separator 
    */ 
    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()); 
      /* 
      * use createDefaultOptions() in place of fsOptions for all default 
      * options - Ashok. 
      */ 

      // 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(); 
     } 
    } 

    public static boolean move(String hostName, String username, String password, String remoteSrcFilePath, String remoteDestFilePath){ 
     StandardFileSystemManager manager = new StandardFileSystemManager(); 

     try { 
      manager.init(); 

      // Create remote object 
      FileObject remoteFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteSrcFilePath), createDefaultOptions()); 
      FileObject remoteDestFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteDestFilePath), createDefaultOptions()); 

      if (remoteFile.exists()) { 
       remoteFile.moveTo(remoteDestFile);; 
       System.out.println("Move remote file success"); 
       return true; 
      } 
      else{ 
       System.out.println("Source file doesn't exist"); 
       return false; 
      } 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } finally { 
      manager.close(); 
     } 
    } 

    /** 
    * Method to download the file from remote server location 
    * 
    * @param hostName 
    *   HostName of the server 
    * @param username 
    *   UserName to login 
    * @param password 
    *   Password to login 
    * @param localFilePath 
    *   LocalFilePath. Should contain the entire local file path - 
    *   Directory and Filename with \\ as separator 
    * @param remoteFilePath 
    *   remoteFilePath. Should contain the entire remote file path - 
    *   Directory and Filename with/as separator 
    */ 
    public static void download(String hostName, String username, String password, String localFilePath, String remoteFilePath) { 

     StandardFileSystemManager manager = new StandardFileSystemManager(); 

     try { 
      manager.init(); 

      // Append _downlaod_from_sftp to the given file name. 
      //String downloadFilePath = localFilePath.substring(0, localFilePath.lastIndexOf(".")) + "_downlaod_from_sftp" + localFilePath.substring(localFilePath.lastIndexOf("."), localFilePath.length()); 

      // Create local file object. Change location if necessary for new downloadFilePath 
      FileObject localFile = manager.resolveFile(localFilePath); 

      // 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(); 
     } 
    } 

    /** 
    * Method to delete the specified file from the remote system 
    * 
    * @param hostName 
    *   HostName of the server 
    * @param username 
    *   UserName to login 
    * @param password 
    *   Password to login 
    * @param localFilePath 
    *   LocalFilePath. Should contain the entire local file path - 
    *   Directory and Filename with \\ as separator 
    * @param remoteFilePath 
    *   remoteFilePath. Should contain the entire remote file path - 
    *   Directory and Filename with/as separator 
    */ 
    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: 
    /** 
    * Method to check if the remote file exists in the specified remote 
    * location 
    * 
    * @param hostName 
    *   HostName of the server 
    * @param username 
    *   UserName to login 
    * @param password 
    *   Password to login 
    * @param remoteFilePath 
    *   remoteFilePath. Should contain the entire remote file path - 
    *   Directory and Filename with/as separator 
    * @return Returns if the file exists in the specified remote location 
    */ 
    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(); 
     } 
    } 

    /** 
    * Generates SFTP URL connection String 
    * 
    * @param hostName 
    *   HostName of the server 
    * @param username 
    *   UserName to login 
    * @param password 
    *   Password to login 
    * @param remoteFilePath 
    *   remoteFilePath. Should contain the entire remote file path - 
    *   Directory and Filename with/as separator 
    * @return concatenated SFTP URL string 
    */ 
    public static String createConnectionString(String hostName, String username, String password, String remoteFilePath) { 
     return "sftp://" + username + ":" + password + "@" + hostName + "/" + remoteFilePath; 
    } 

    /** 
    * Method to setup default SFTP config 
    * 
    * @return the FileSystemOptions object containing the specified 
    *   configuration options 
    * @throws FileSystemException 
    */ 
    public static FileSystemOptions createDefaultOptions() throws FileSystemException { 
     // Create SFTP options 
     FileSystemOptions opts = new FileSystemOptions(); 

     // SSH Key checking 
     SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); 

     /* 
     * Using the following line will cause VFS to choose File System's Root 
     * as VFS's root. If I wanted to use User's home as VFS's root then set 
     * 2nd method parameter to "true" 
     */ 
     // Root directory set to user home 
     SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false); 

     // Timeout is count by Milliseconds 
     SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000); 

     return opts; 
    } 
} 
関連する問題