2014-05-09 32 views
3

以下是我的程序......但這不是session.connect()Jsch示例文件複製到SFTP服務器

public static void main(String args[]) 
     { 
     try { 
      String ftpHost = "XXXXXXXX"; 
      int ftpPort = 21;  
      String ftpUserName = "XXXX"; 
      String ftpPassword = "XXXXX"; 
      String ftpRemoteDirectory = "/"; 
      String fileToTransmit = "C://XXXXX//Desktop//RG//10171699_821972117859158_5724612734096298046_n.jpg";   
      JSch.setLogger(new MyLogger()); 
      System.out.println("Creating session."); 
      JSch jsch = new JSch(); 

      Session session = null; 
      Channel channel = null; 
      ChannelSftp c = null; 

      // 
      // Now connect and SFTP to the SFTP Server 
      // 
      try { 
       // Create a session sending through our username and password 
       session = jsch.getSession(ftpUserName, ftpHost, ftpPort); 
       System.out.println("Session created."); 
       session.setPassword(ftpPassword); 

       java.util.Properties config = new java.util.Properties(); 
       config.put("StrictHostKeyChecking", "no"); 
       session.setConfig(config); 
       System.out.println("Session connected before."); 
       session.connect(); 
       System.out.println("Session connected."); 

       System.out.println("OPEN SFTP CHANNEL"); 
       // 
       // Open the SFTP channel 
       // 
       System.out.println("Opening Channel."); 
       channel = session.openChannel("sftp"); 
       channel.connect(); 
       c = (ChannelSftp) channel; 
       System.out.println("Now checing status"); 
      } catch (Exception e) { 
       System.err.println("Unable to connect to FTP server." 
         + e.toString()); 
       throw e; 
      } 

      // 
      // Change to the remote directory 
      // 
      System.out.println("Now performing operations"); 
      ftpRemoteDirectory="/home/pooja111/"; 
      System.out.println("Changing to FTP remote dir: " 
        + ftpRemoteDirectory); 
      c.cd(ftpRemoteDirectory); 

      // 
      // Send the file we generated 
      // 
      try { 
       File f = new File(fileToTransmit); 
       System.out.println("Storing file as remote filename: " 
         + f.getName()); 
       c.put(new FileInputStream(f), f.getName()); 
      } catch (Exception e) { 
       System.err 
         .println("Storing remote file failed." + e.toString()); 
       throw e; 
      } 

      // 
      // Disconnect from the FTP server 
      // 
      try { 
       c.quit(); 
      } catch (Exception exc) { 
       System.err.println("Unable to disconnect from FTPserver. " 
         + exc.toString()); 
      } 

     } catch (Exception e) { 
      System.err.println("Error: " + e.toString()); 
     } 

     System.out.println("Process Complete."); 
     System.exit(0); 
    } 

和輸出

Creating session. 
Session created. 
Session connected before. 
INFO: Connecting to ftp.olstr.com port 21 
INFO: Connection established 

我的代碼控制後工作在session.connect()行之後沒有移動。

+0

[JSCH:SFTP可能的重複。掛在session.connect()使用端口21](http://stackoverflow.com/questions/20559448/jsch-sftp-hangs-at-session-connect-using-the-port-21) –

+0

你是服務器試圖連接是'ftp'還是'sftp'? –

+0

默認端口:'ftp = 21,sftp = 22' –

回答

5

不同的連接有不同的端口,如FTP,FTPS,SFTP,SSH上的FTP。使用適當的端口。這些是港口。 20個FTP數據(文件傳輸協議),21個FTP(文件傳輸協議),22個SSH(安全外殼)。 Use session.connect(timeout)。評論你的堆棧跟蹤,以便我知道什麼是確切的錯誤。試試22,看看錯誤是否仍然存在。

+0

謝謝,我不停地檢查我的jscp代碼,使用端口21,不知道該sftp強制端口22.謝謝! –

+1

它不是任務。它是默認的端口。你可以配置它。 –

相關問題