I am using Apache common.net package 2.0 for my FTP file transfer from one server to another server. And i'm using eclipse 3.3.2 IDE for my code execution.
Following is the function i'm using for ftp to ftp file transfer.
public void severtoservercopyfile()
{
FTPClient ftpClient1 = new FTPClient();
FTPClient ftpClient2 = new FTPClient();
ProtocolCommandListener listener;
try
{
listener = new PrintCommandListener(new PrintWriter(System.out));
ftpClient1.addProtocolCommandListener(listener);
ftpClient2.addProtocolCommandListener(listener);
System.out.println("Connecting FTP1 ");
ftpClient1.connect("ftp.xxxx.com");
ftpClient1.login("demo", "demo");
if (!FTPReply.isPositiveCompletion(ftpClient1.getReplyCode())) {
String errMsg = "User : ftp1 server could not connect. FTP server refused connection.";
System.out.println(errMsg);
}
System.out.println("Connecting FTP2 ");
ftpClient2.connect("xx.xx.xxx.xx");
ftpClient2.login("username", "password");
if (!FTPReply.isPositiveCompletion(ftpClient2.getReplyCode())) {
String errMsg = "User : could not connect. FTP2 server refused connection.";
System.out.println(errMsg);
}
String fromPath = "/PPP/Test/";
String toPath = "/testprojects/PPP/Test/";
String file1 = "2009121.xml";
String file2 = "2009121.xml";
int replyCode = ftpClient1.cwd(fromPath);
System.out.println("replyCode 1 : "+replyCode);
if (!FTPReply.isPositiveCompletion(replyCode)) {
String errMsg = "Could not change to directory : "
+ fromPath;
System.out.println(errMsg);
}
int replyCode2 = ftpClient2.cwd(toPath);
System.out.println("replyCode 2 : "+replyCode2);
if (!FTPReply.isPositiveCompletion(replyCode2)) {
String errMsg = "Could not change to directory : "
+ toPath;
System.out.println(errMsg);
}
//_main:
try{
boolean isPassive = ftpClient2.enterRemotePassiveMode();
boolean isActive = false;
if(isPassive){
System.out.println("FtpClient2 is in Passive mode");
System.out.println("FtpClient2 Host : "+ftpClient2.getPassiveHost());
System.out.println("FtpClient2 Port : "+ftpClient2.getPassivePort());
System.out.println("FtpClient1 is entering RemoteActive Port : ");
isActive = ftpClient1.enterRemoteActiveMode(InetAddress.getByName(ftpClient2.getPassiveHost()), ftpClient2.getPassivePort());
if(isActive)
{
System.out.println("FtpClient1 is in Active mode");
}
else
{
System.out.println("FtpClient1 is NOT in Active mode");
}
System.out.println(" FtpClient1 Remote verification : "+ftpClient1.isRemoteVerificationEnabled());
System.out.println(" FtpClient2 Remote verification : "+ftpClient2.isRemoteVerificationEnabled());
}
else
{
System.out.println("FtpClient2 is NOT in Passive mode");
}
/*boolean remoteRet = ftpClient1.remoteRetrieve(file1);
boolean remoteStore = ftpClient2.remoteStoreUnique(file2);
ftpClient1.completePendingCommand();
ftpClient2.completePendingCommand();
System.out.println("FtpClient1 remote Retrive "+remoteRet);
System.out.println("FtpClient2 remote Store "+remoteStore);*/
if (ftpClient1.remoteRetrieve(file1) && ftpClient2.remoteStoreUnique(file2))
{
System.out.println("File Transferring ...");
//if(ftp1.remoteRetrieve(file1) && ftp2.remoteStore(file2)) {
// We have to fetch the positive completion reply.
ftpClient1.completePendingCommand();
ftpClient2.completePendingCommand();
}
else
{
System.out.println("Couldn't initiate transfer. Check that filenames are valid.");
//break _main;
}
} catch(Exception e){
}
System.out.println(replyCode);
System.out.println("Disconnecting FTP1");
ftpClient1.disconnect();
System.out.println("Disconnecting FTP2");
ftpClient2.disconnect();
System.out.println("Finished FTP");
} catch(Exception e){
System.out.println("Exception Occured");
if (ftpClient1.isConnected())
{
try
{
ftpClient1.disconnect();
}
catch (IOException f)
{
// do nothing
}
}
if (ftpClient2.isConnected())
{
try
{
ftpClient2.disconnect();
}
catch (IOException f)
{
// do nothing
}
}
System.out.println("Error : "+e);
}
}
I'm getting the error "425: Can't build data connection" and part of my console display is
FtpClient1 is in Active mode
FtpClient1 Remote verification : true
FtpClient2 Remote verification : true
RETR 2009121.xml
150 Opening BINARY mode data connection for 2009121.xml (543 bytes).
STOU 2009121.xml
425: Can't build data connection.
Couldn't initiate transfer. Check that filenames are valid.
250
Can anybody tell me how to overcome this error and make this code to run successfully.