我正在实现一个Java程序,
- 必须连接到远程服务器
- 连接的远程服务器应该从 ftp 下载文件
我正在为这段代码使用 Apache MINA lib
这是代码,它连接到远程服务器
public class filetrans
{
public static void main(String[] args) throws IOException, InterruptedException
{
SshClient client = null;
String login="user";
String password="password";
try
{
client = SshClient.setUpDefaultClient();
client.start();
ConnectFuture future = client.connect("myhost",myport);
future.await();
ClientSession session = (ClientSession) future.getSession();
boolean auth = session.authPassword(login, password).await().isSuccess();
if (auth)
{
System.out.println("Authenticated....");
ClientChannel channel = session.createChannel("shell");
channel.setIn(new NoCloseInputStream(System.in));
channel.setOut(new NoCloseOutputStream(System.out));
channel.setErr(new NoCloseOutputStream(System.err));
channel.open();
channel.waitFor(ClientChannel.CLOSED, 5000);
channel.close(true);
}
else
{
System.out.println("Authentication failed....");
}
}
catch (Throwable t)
{
System.out.println(t);
}
finally
{
client.stop();
}
}
}
我成功连接到远程服务器。现在我必须连接到 FTP 服务器并下载文件并保存在远程服务器中。我被困在这里,任何如何进一步实施的想法或任何代码或任何建议都会很棒。谢谢