Ftp and Java

Originally posted on April 17th, 2005 on another blog, here is a brief example on how to use the jvftp library for sending files via ftp in the java language.

PrintStream oldOut = System.out;
PrintStream oldErr = System.err;
Ftp client = new Ftp();
FtpContext context = client.getContext();

//redirecting System.out/err to log4j appender
System.setOut(new PrintStream(new LoggingOutputStream(logger,Level.INFO),true));
System.setErr(new PrintStream(new LoggingOutputStream(logger,Level.ERROR),true));

context.setFileTransferMode('A');

try{
   client.connect(connectionParams.getAddress(),Ftp.PORT);
   client.login(connectionParams.getUser(),connectionParams.getPassword());
   client.command("site recfm=FB lrecl=256");

   CoFile from = new LocalFile(connectionParams.getFile());
   CoFile to = new FtpFile("'" + connectionParams.getFile() + "'",client);
   CoLoad.copy(to,from);
}catch(IOException ioe){
   logger.error("save() IOException",ioe);
   throw new ExecuteCommandException(ioe.getMessage());
}finally{
client.disconnect();
}

//restoring original System.out/err
System.setOut(oldOut);
System.setErr(oldErr);

logger.debug("done");

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s