2014-05-09 35 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

它不是任务。它是默认的端口。你可以配置它。 –

相关问题