Source Code : Java FTP Upload Example using Apache Commons Net API

Java FTP Upload Example using Apache Commons Net API

package com.journaldev.inheritance;

import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter;

import org.apache.commons.net.PrintCommandListener; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply;

public class FTPUploader {

FTPClient ftp = null;

public FTPUploader(String host, String user, String pwd) throws Exception{

ftp = new FTPClient();

ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

int reply;

ftp.connect(host);

reply = ftp.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply)) {

ftp.disconnect();

throw new Exception("Exception in connecting to FTP Server");

}

ftp.login(user, pwd);

ftp.setFileType(FTP.BINARY_FILE_TYPE);

ftp.enterLocalPassiveMode();

}

public void uploadFile(String localFileFullName, String fileName, String hostDir)

throws Exception {

try(InputStream input = new FileInputStream(new File(localFileFullName))){

this.ftp.storeFile(hostDir + fileName, input);

}

}

public void disconnect(){

if (this.ftp.isConnected()) {

try {

this.ftp.logout();

this.ftp.disconnect();

} catch (IOException f) {

// do nothing as file is already saved to server   }

}

}

public static void main(String[] args) throws Exception {

System.out.println("Start");

FTPUploader ftpUploader = new FTPUploader("ftp.journaldev.com", "ftpUser", "ftpPassword");

//FTP server path is relative. So if FTP account HOME directory is "/home/pankaj/public_html/" and you need to upload    // files to "/home/pankaj/public_html/wp-content/uploads/image2/", you should pass directory parameter as "/wp-content/uploads/image2/"   ftpUploader.uploadFile("D:\\Pankaj\\images\\MyImage.png", "image.png", "/wp-content/uploads/image2/");

ftpUploader.disconnect();

System.out.println("Done");

}

}

You can use above program to connect to FTP server and then upload files to the server. Make sure you provide FTP Host, user and password details correctly in the program. You can get these details when you create an FTP user.

The other important point to note is the server directory location and it’s relative to the FTP user home directory. Also once you are done with uploading all the files, close the connection to server and release resources.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.concurrent.ExecutionException;

//import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPUploader {

FTPClient ftp = null;

public FTPUploader(String host, String user, String pwd) throws Exception{
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.connect(host);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new Exception("Exception in connecting to FTP Server");
}
ftp.login(user, pwd);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
}
public void uploadFile(String localFileFullName, String fileName, String hostDir)
throws Exception {
try {
InputStream input = new FileInputStream(new File(localFileFullName));

this.ftp.storeFile(hostDir + fileName, input);
}
catch(Exception e){

}
}

public void disconnect(){
if (this.ftp.isConnected()) {
try {
this.ftp.logout();
this.ftp.disconnect();
} catch (IOException f) {
// do nothing as file is already saved to server
}
}
}
public static void main(String[] args) throws Exception {
System.out.println("Start");
FTPUploader ftpUploader = new FTPUploader("sftp.p.com", "user", "Pwd");

//FTP server path is relative. So if FTP account HOME directory is "/home/pankaj/public_html/" and you need to upload
// files to "/home/pankaj/public_html/wp-content/uploads/image2/", you should pass directory parameter as "/wp-content/uploads/image2/"
ftpUploader.uploadFile("C:\\Users\\Kotesh\\Desktop\\Skills\\uploadedFile.txt", "uploadedFile.txt", "/HOME/DBDUMP/");
ftpUploader.disconnect();
System.out.println("Done");
}

}