简体   繁体   中英

How to download file from one Amazon EC2 instance to another EC2 instance using java apache FTPClient

I have setup a ftp server in one amazon EC2 instance and can download file from that server in my local machine using apache ftp library. Now I am trying to run the same ftpclient program in another Amazon EC2 instance, but it is not working. Here is the sample code to download a file from 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();
}

The following code can list all the file names of the remote directory:

for(FTPFile f: ftp.listFiles(remote))
{
  System.out.println(f.getName());

}

Both of the code work fine in my local machine but is not working in amazon machine. It connects but cannot list the file name or download file. And it doesn't show any error message.

Thanks in advance--

I just found a possible answer, because I have been having the same problem for a while. It solved the problem to me.

You have to add this line to your code: ftp.enterLocalPassiveMode();

after connect() and before login().

Here is the link: http://bartling.blogspot.dk/2012/04/using-apache-commons-net-ftpclient-on.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM