![](/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.