2015-09-23 33 views
0

我需要发送以下ftp命令如何使用JSch API发送UNIX ftp命令?

% ftp test.cs.xxxx.edu 
Connected to test.cs.xxxx.edu. 
220 test FTP server (Version 5.53 ........) ready. 
Name (test.cs.xxxx.edu:yourlogin): yourlogin 
331 Password required for yourlogin. 
Password: 
230 User yourlogin logged in. 
ftp> cd HPSC/exercises 
ftp> get JHFLKDHLFKD.zip 

我试图发送这些命令如下

JSch jsch = new JSch();  

Session session_1 = jsch.getSession("user1", "host1", 22); 
session_1.setConfig("StrictHostKeyChecking", "no"); 
session_1.setPassword("pass1"); 
session_1.connect(); 
System.out.println("The session 1 has been established"); 

ChannelExec exec = (ChannelExec)session_1.openChannel("exec"); 

exec.connect(); 
exec.setCommand("ftp test.cs.xxxx.edu"); 
exec.setCommand("user2"); 
exec.setCommand("pass2"); 
exec.setCommand("cd \test\test\test"); 
exec.setCommand("get JHFLKDHLFKD.zip"); 

但没有奏效。我也尝试过“壳”。

我在做什么错?

+0

提示:当您启动FTP客户端时,您认为如何解释FTP命令? shell还是FTP客户端? – fge

回答

0

您要发送的命令是ftp进程的命令。这些不是远程shell可以理解的命令。

唯一真正的命令是ftp。剩下的部分必须送到启动的过程以达到其标准输入。使用setInputStream method提供输入。


无论如何,你应该更好地使用本地FTP客户端,而该开始在服务器端的命令行客户端,如我建议你以前:
How to connect to an FTP server from an existing connection to Unix server?

+0

谢谢。我尝试了FTP客户端,但总是得到'拒绝连接', – Pasha

0

您正在尝试使用一个SFTP客户端库与FTP服务器进行通信。

尽管名称和用途相似,但FTP和SFTP是完全不同的协议。请参阅 Differences between SFTP and "FTP over SSH"

您需要更改服务器或客户端,以便它们匹配。