Code to transfer files through FTP

This code copies file from a local drive to a remote drive using FTP protocol

 
package com.test.ftp;
 
import java.io.File;
import java.io.FileInputStream;
 
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
 
public class FTPTest {
public static void main(String args[]) {
try{
FTPClient ftp = new FTPClient();
ftp.connect("ftp://10.10.10.42/"); //ip address
 
ftp.login ("username", "password"); // Valid User Name & password
ftp.enterLocalPassiveMode();
ftp.setFileType(org.apache.commons.net.ftp.FTP.IMAGE_FILE_TYPE);
 
int reply = ftp.getReplyCode();
FileInputStream local = new FileInputStream("D:\\test.pdf");
/** Location in FTP **/
boolean f = ftp.storeFile ("Ram" + File.separator + "D:\\test.pdf" , local);
 
System.out.println(f);
if(!f) {
String data = "Ram" + File.separator + "D:\\test.pdf";
//logger.debug( "Unable to upload file " + data );
local.close();
}
local.close();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
 
}
 
ftp.logout();
ftp.disconnect();
}catch(Exception e){
 
}
}
}





About this entry