Thursday, November 29, 2007

copying files from one FTP Server to another

import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import org.apache.commons.net.ProtocolCommandListener;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.log4j.Logger;

public class FTP {
Logger logger = Logger.getLogger(FTP .class);
private static String ftpHost="HOST_SOURCE";
private static String userName_sou="username_source";
private static String password_sou="password_source";
private static String directoryLocation_sou="/directoryname";
private static String ftpHost_dest="HOST_DEST";
private static String userName_dest="username_dest";
private static String password_dest="password_dest";
private static String directoryLocation_dest="/directoryname";
public static void main(String[] arg)
{
new FTPClientTest().getFileFromServerLocation();
}
public boolean getFileFromServerLocation()
{
FTPClient client_sou = new FTPClient();
FTPClient client_dest = new FTPClient();
boolean status = false;
boolean connected = false;
ProtocolCommandListener listner=new FrpClientListener();
try
{
// client_sou.addProtocolCommandListener(listner);
// client_dest.addProtocolCommandListener(listner);
//destination directory - start
InetAddress address_dest = InetAddress.getByName(ftpHost_dest);
logger.debug("Connecting to ftp server using " + ftpHost_dest);
// Connect to the host..
client_dest.connect(address_dest);
connected = true;
logger.debug("Loggin on to server using user '" + userName_dest
+ "' and password *********");
if (this.password_dest == null)
{
}
boolean flag_dest = client_dest.login(this.userName_dest, this.password_dest);
logger.debug("Log on status " + flag_dest);
logger.debug("Reply String: " + client_dest.getReplyString());
logger.debug("Working dir:" + client_dest.printWorkingDirectory());
// get the current working dir.
String workingDir_dest = client_dest.printWorkingDirectory();
String currworkingDir_dest = client_dest.printWorkingDirectory()+directoryLocation_dest;
boolean workingDirStatus_dest = client_dest.changeWorkingDirectory(workingDir_dest
+ directoryLocation_dest);
//destination directory - end
// source directory - start
InetAddress address_sou = InetAddress.getByName(ftpHost);
logger.debug("Connecting to ftp server using " + ftpHost);
// Connect to the host..
client_sou.connect(address_sou);
connected = true;
logger.debug("Loggin on to server using user '" + userName_sou
+ "' and password *********");
if (this.password_sou == null)
{
}
boolean flag = client_sou.login(this.userName_sou, this.password_sou);
logger.debug("Log on status " + flag);
logger.debug("Reply String: " + client_sou.getReplyString());
logger.debug("Working dir:" + client_sou.printWorkingDirectory());
// get the current working dir.
String workingDir_sou = client_sou.printWorkingDirectory();
String currworkingDir = client_sou.printWorkingDirectory()+directoryLocation_sou;
// change the current working dir to the specified dir..
boolean workingDirStatus = client_sou.changeWorkingDirectory(workingDir_sou
+ directoryLocation_sou);
if (!workingDirStatus)
{
}
logger.debug("Current working dir:"
+ client_sou.printWorkingDirectory());
// source directory - end
this.logger.debug("Uploading file to server......");
String file="";
FTPFile[] files = client_sou.listFiles();
for (int i = 0; i < files.length; i++)
{
FTPFile f = files[i];
file = f.getName();
client_dest.enterRemotePassiveMode();
client_sou.enterRemoteActiveMode(InetAddress.getByName(client_dest.getPassiveHost()),
client_dest.getPassivePort());
if (client_sou.remoteRetrieve(file) && client_dest.remoteStore(file))
{
// if(ftp1.remoteRetrieve(file1) && ftp2.remoteStore(file2)) {
// We have to fetch the positive completion reply.
client_sou.completePendingCommand();
client_dest.completePendingCommand();
}

}
logger.debug("File transfer status " + flag);
client_sou.sendNoOp();
status = true;
}
catch (UnknownHostException une)
{
une.printStackTrace();
}
catch (SocketException se)
{
se.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
catch (Throwable e)
{
e.printStackTrace();
}
finally
{
try
{
if (connected)
{
this.logger
.debug("Logout ftp connection and disconnecting..");
client_sou.logout();
client_sou.disconnect();
client_dest.logout();
client_dest.disconnect();
this.logger.debug("Logout successful.");
}
}
catch (IOException e)
{
this.logger.error(e.getMessage(), e);
e.printStackTrace();
}
}
return status;
}
}

1 comment:

Vivek Athalye said...

hey dear... thanks a lot for giving a chance to contact u :)

i've been trying to contact u for more than 2 months now (unfortunately my net was down for last 1 month) ... i've been keeping watch on ur splashpress blog :) ... u didn't give any option to contact u on that blog...

now u must be wondering who am i... why was i trying to contact u... and how did i get to know about ur blog, at first place .... :)

the answer is ... i wanted to create a blog where i'll post all the weird/unusual things that i come to know about... and i had thought of a name "Aisa Bhi Hota Hai" ... now u know why i wanted to contact u :)
looks like u have renamed that blog as "the-techie-splashpress" ... which has allowed me to register "Aisa-Bhi-Hota-Hai"... thanks a lot for that :)

i don't know u personally... nor have u mentioned ur real name anywhere on the blog... so i'll remember u as cindrella .. may b a fat cindrella ;).

btw, congrats for getting engaged and i wish you good luck for ur married life.
(just wondering, if ur fiance has read all ur posts or not :D )

bbye,
tc
-Vivek

you can delete this comment once u've read it, after all it's not related to your java code :)

REFACTORING

 What is Refactoring? A software is built initially to serve a purpose, or address a need. But there is always a need for enhancement, fixin...