![](/img/trans.png)
[英]How to move an image file from amazon s3 to amazon ec2 and run the instance using java
[英]How to download file from one Amazon EC2 instance to another EC2 instance using java apache FTPClient
我已经在一个Amazon EC2实例中设置了一个ftp服务器,并且可以使用apache ftp库从本地计算机中的该服务器下载文件。 现在,我试图在另一个Amazon EC2实例中运行相同的ftpclient程序,但是它不起作用。 这是从ftpsever下载文件的示例代码:
FTPClient ftp = new FTPClient();
String loc = "/home/ubuntu/test/";
String remote = "/home/ftp";
try
{
ftp.connect("ec2-xx-xx-xx-xxx.compute-1.amazonaws.com", 21);
ftp.login("username", "xxx");
System.out.println("connected..");
ftp.setFileType(FTP.BINARY_FILE_TYPE);
OutputStream output;
output = new FileOutputStream(loc+"file_name");
ftp.retrieveFile(remote+"/filen_name", output);
output.close();
ftp.disconnect();
}
catch(Exception ex)
{
ex.printStackTrace();
}
以下代码可以列出远程目录的所有文件名:
for(FTPFile f: ftp.listFiles(remote))
{
System.out.println(f.getName());
}
两种代码都可以在我的本地计算机上正常运行,但是在亚马逊计算机上则无法运行。 它可以连接,但无法列出文件名或下载文件。 而且它不显示任何错误消息。
提前致谢 -
我刚刚找到了一个可能的答案,因为一段时间以来我一直遇到相同的问题。 它解决了我的问题。
您必须将此行添加到代码中:ftp.enterLocalPassiveMode();
在connect()之后和login()之前。
这是链接: http : //bartling.blogspot.dk/2012/04/using-apache-commons-net-ftpclient-on.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.