How to upload files into FTP server using java

         
  1. FTP(File Transfer Protocol) is used for sharing the files from one location to another. its fallows client-server architecture.
  2. First, create the properties class for loading the connection properties to the FTP client object.
  3. Create the ftpClient object and using the connect method to connect FTP server pass the properties class object as a parameter.
  4. Use the storeFile method to upload the file into FTP Server.
  5. Use Logout or disconnect methods to close the FTP connection.
Flow
Here I am creating the External Properties for dynamic changes effect. Whenever change the server or file details no need to change, compile and run the file. Also, I am creating the logger file for error checking while Executing the code. below are files 
FileTransfer.java
/** * * @author Janardhan Randhi * Date : Feb 5 2019 * Description : This class get the local file and upload into ftp directory and delete the local file. * */ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.apache.log4j.Logger; import java.util.Properties; import java.io.FileReader; public class FileTransfer { private static final Logger LOGGER = Logger.getLogger(FileTransfer.class.getName()); private static void showServerReply(FTPClient ftpClient) { String[] replies = ftpClient.getReplyStrings(); if (replies != null && replies.length > 0) { for (String aReply : replies) { LOGGER.info("SERVER: " + aReply); } } } public static void main(String[] args) throws FileNotFoundException, IOException { FileReader reader=new FileReader("FileTransfer.properties"); Properties p=new Properties(); p.load(reader); File f = new File(p.getProperty("LocalSystemFilePath")); //local system file path String name = f.getName(); FileInputStream fis = new FileInputStream(f); FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(p.getProperty("server"), Integer.parseInt(p.getProperty("port"))); showServerReply(ftpClient); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { LOGGER.info("Operation failed. Server reply code: " + replyCode); System.exit(0); } boolean success = ftpClient.login(p.getProperty("user"), p.getProperty("pass")); showServerReply(ftpClient); if (!success) { LOGGER.info("Could not login to the server"); System.exit(0); } else { LOGGER.info("LOGGED IN SERVER"); ftpClient.storeFile(name, fis); //push the file into ftp directory. ftpClient.logout(); //logout from the ftp server. ftpClient.disconnect(); //disconnect from the ftp server. fis.close(); //Close the file inputstream. f.delete(); //Delete the file from local system. System.out.println("File Transfer completed"); LOGGER.info("File transfer complete and delete the file from local path...!!"); } } catch (IOException ex) { LOGGER.error(ex.getMessage()); } } }
FileTransfer.properties
LocalSystemFilePath =C:\\Users\\Janardhan\\Desktop\\Checking\\sample.txt server=qucik.devops.com port=21 user=janardhan pass=Randhi
Log4J.properties
# Root logger option log4j.rootLogger=INFO, file, stdout # configuration to print into file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=C:\\FileTransfer\\FileTransfer.log log4j.appender.file.MaxFileSize=12MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # configuration to print on console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

SUBSCRIBE TO OUR NEWSLETTER

I’m the Founder of quickdevops.com. I am a Professional Blogger, Application developer, YouTuber. I’ve been blogging since 2015.I spend a lot of time learning new techniques and actively help other people learn web development through a variety of help groups and writing web development tutorials for my website and blog about advancements in web design and development.Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie or planning a trip to someplace I've never been before.

1 Response to "How to upload files into FTP server using java"