2016-12-14 579 views
0

我想開發一個Java程序,將文件從SFTP服務器下載到遠程服務器。該遠程服務器沒有任何共享路徑。我必須直接從sftp下載並粘貼到遠程Windows服務器驅動程序(D :)中。將SFTP文件直接下載到遠程服務器文件夾(不是共享路徑)

代碼:

int ftpPort = 22; 

    JSch jsch = new JSch (); 
     Session session = null; 
     Channel channel = null; 
     ChannelSftp c = null; 

     try { 

     session = jsch.getSession(username, hostname, ftpPort); 
     logger.info("*** FTP Session created. ***"); 
     session.setPassword(password); 

     Properties config = new Properties(); 
     config.put("StrictHostKeyChecking", "no"); 
     session.setConfig(config); 
     session.connect(); 
     logger.info("*** Session connected. ***"); 

     //Open the SFTP channel 
     logger.info("*** Opening FTP Channel. ***"); 
     channel = session.openChannel("sftp"); 
     channel.connect(); 
     c = (ChannelSftp) channel; 

     //Change to the remote directory 
     logger.info("*** Changing to FTP remote dir: " + remoteDirectory + " ***"); 
     c.cd(remoteDirectory); 

     //Send the file we generated 
     try { 
       logger.info("*** Storing file:'" + filename + "' to local directory: '"+localDirectory+"'"); 

我使用Jsch和ChannelSftp連接到SFTP服務器。

截至目前,上述代碼將代碼下載到本地路徑和共享路徑。

任何建議將文件下載到不具有任何共享路徑的遠程服務器(Windows)。

謝謝。

回答

1

您的代碼需要直接在遠程服務器上運行,並將文件從SFTP下載到本地磁盤。

另一種方法是使用您的代碼在本地計算機上下載文件,然後使用類似SCP的文件在遠程服務器上傳輸文件(如果您確實沒有任何共享文件夾)。

scp /path/to/your/file [email protected]:/remote/path 

但你說,這是Windows系統,因此你可能需要先設置SSH/SCP那臺機器上。

相關問題