2015-05-22 311 views
0

我想通过SFTP传输文件。我发现一个类似的问题,从how to transfer a file through SFTP in java?询问。我在本地主机上试用了推荐的解决方案。但在输出下面显示以下错误。通过SFTP传输文件

输出

preparing the host information for sftp. 
Host connected. 
sftp channel opened and connected. 
2: No such file 
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846) 
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2340) 
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:342) 
at ScpTo.main(ScpTo.java:69) 
File transfered successfully to host. 
sftp Channel exited. 
Channel disconnected. 
Host Session disconnected. 

代码

import java.io.File; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 

import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelSftp; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.JSchException; 
import com.jcraft.jsch.Session; 
import com.jcraft.jsch.SftpException; 

public class ScpTo { 

    /** 
    * 
    */ 
    public ScpTo() { 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     String SFTPHOST = "192.168.1.3"; 
      int SFTPPORT = 22; 
      String SFTPUSER = "sam-PC"; 
      String SFTPPASS = ""; 
      String SFTPWORKINGDIR = "C:/Users/sam/Desktop/hi.txt"; 

      Session session = null; 
      Channel channel = null; 
      ChannelSftp channelSftp = null; 
      System.out.println("preparing the host information for sftp."); 

       JSch jsch = new JSch(); 
       try { 
        session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT); 
       } catch (JSchException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       session.setPassword(SFTPPASS); 
       java.util.Properties config = new java.util.Properties(); 
       config.put("StrictHostKeyChecking", "no"); 
       session.setConfig(config); 
       try { 
        session.connect(); 
       } catch (JSchException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       System.out.println("Host connected."); 
       try { 
        channel = session.openChannel("sftp"); 
       } catch (JSchException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       try { 
        channel.connect(); 
       } catch (JSchException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       System.out.println("sftp channel opened and connected."); 
       channelSftp = (ChannelSftp) channel; 
       try { 
        channelSftp.cd(SFTPWORKINGDIR); 
       } catch (SftpException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       File f = new File(SFTPWORKINGDIR); 
       try { 
        channelSftp.put(new FileInputStream(f), f.getName()); 
       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (SftpException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       System.out.println("File transfered successfully to host."); 


       channelSftp.exit(); 
       System.out.println("sftp Channel exited."); 
       channel.disconnect(); 
       System.out.println("Channel disconnected."); 
       session.disconnect(); 
       System.out.println("Host Session disconnected."); 

    } 
} 
+1

从**开始**不是一次捕获所有异常。知道你想要做什么,在寻求帮助之前缩小自己的问题。 – zubergu

+0

发布已更新。这似乎是它与\t \t \t \t \t \t session.connect问题(); – user2211678

+1

你是否启动了服务器?尝试运行'netstat -ano'并检查是否有一个进程正在侦听端口22. – longhua

回答

2

堆栈-跟踪显示cd命令的执行期间发生的错误。错误是“没有这样的文件”,这可能实际上意味着“没有这样的目录”,所以问题几乎可以肯定的是由SFTPWORKINGDIR定义的目录不存在。

,我不怪你混淆,虽然。该API似乎非常复杂。我是edtFTPj/PRO的开发人员之一,它提供了一个更简单的界面。例如,下面是通过SFTP下载文件的代码:

SecureFileTransferClient client = new SecureFileTransferClient(); 
client.setProtocol(Protocol.SFTP); 
client.setRemoteHost("myserver.com"); 
client.setUserName("myusername"); 
client.setPassword("mypassword"); 
client.connect(); 
client.downloadFile("filename.txt", "filename.txt"); 
client.disconnect(); 

请注意,edtFTPj/PRO不是免费的产品。

1
String SFTPWORKINGDIR = "C:/Users/sam/Desktop/hi.txt"; 
... 
channelSftp.cd(SFTPWORKINGDIR); 

首先,SFTPWORKINGDIR似乎是指一个文件,而不是一个目录。您可能会尝试将cd指定为文件,或者应该是文件。

其次,SFTP使用的路径名类似Unix的模型。以“/”开头的路径名称是绝对路径,并从服务器的根目录中解析。不以“/”开头的路径是相对路径,并且从SFTP会话开始的目录(通常是您在服务器上登录的帐户的主目录)解析。

你的SFTPWORKINGDIR值是Windows格式的路径,而不是一个UNIX格式的路径。即使该SFTP服务器在Windows系统上运行,SFTP服务器很可能不会按照您的意图解释它。您应该查阅SFTP服务器的文档并将路径名转换为适当的格式以访问您需要的目录。如果SFTP服务器是Cygwin openssh服务器,我相信正确的形式将类似于“/ cygdrive/c/Users/sam/Desktop”。

0

这是一个完整的“上传代码”与Windows机器到Linux机器的认证。此代码从您的计算机上的特定位置上载文件(win)并上传到FTP服务器。这是我申请我的问题的解决方案。如果有人看到问题,请评论。

try { 
     JSch jsch = new JSch(); 
     Session session = jsch.getSession("<userName>", "<ip_block>", 22); 
     session.setConfig("PreferredAuthentications", "password"); 
     session.setPassword("<pasword>"); 
     java.util.Properties config = new java.util.Properties(); 
     config.put("StrictHostKeyChecking", "no");//do not prefer this. demo only 
     session.setConfig(config); 
     session.connect(1200); 
     Channel channel = session.openChannel("sftp"); 
     ChannelSftp sftp = (ChannelSftp) channel; 
     sftp.connect(600); 
     channel = session.openChannel("sftp"); 
     channel.connect(); 
     try { 
      File f = new File("E:/apache-tomcat-7.0.65/bin/" + this.fileName); 
      sftp.put(new FileInputStream(f), f.getName()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     channel.disconnect(); 
     sftp.disconnect(); 
    } catch (JSchException e) { 
     e.printStackTrace(); 
    }