How to upload files into FTP server using java
- FTP(File Transfer Protocol) is used for sharing the files from one location to another. its fallows client-server architecture.
- First, create the properties class for loading the connection properties to the FTP client object.
- Create the ftpClient object and using the connect method to connect FTP server pass the properties class object as a parameter.
- Use the storeFile method to upload the file into FTP Server.
- Use Logout or disconnect methods to close the FTP connection.
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
Good information
ReplyDelete