In order to start working with Commons Net library add following lines into your Maven enabled project's pom.xml file.
commons-net commons-net 1.0.0
General procedure to start with ftp server is as follows:
1- connect to server
2- login with userName and Password
3- retrieve list of files from specified ftp folder
4- save the file from ftp to local
5- save the file from local to ftp
6- sometimes it is required to check for response codes from ftp
7- complete operations and disconnect
FtpConnect() method connects to remote ftp server, reads file content, save it to local director, then save a copy of it to another remote ftp location.
package apache.commons.net.ftpclient;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpClientService {
public FtpClientService(){
}
public static void FtpConnect(){
String userName="ftpLoginUserName";
String passWord = "ftppass";
String ftpAddress = "ftp.somedomain.com";
String retrieveFromFTPFolder = "RemoteFolderName/";
String saveToLocalFolder = "/LocalFolder/";
String saveToFtpFolder = "/ftpUploadFolder/";
String strLine;
DataInputStream inputStream = null;
BufferedReader bufferedReader = null;
FTPClient client = null;
FTPFile[] ftpFiles = null;
int reply;
try{
client = new FTPClient();
client.connect(ftpAddress);
client.login(userName, passWord);
ftpFiles = client.listFiles(retrieveFromFTPFolder); //retrieve list of files from ftp server
if (ftpFiles != null && ftpFiles.length > 0) {
for (FTPFile file : ftpFiles) { // get specified file from folder
if(file.isFile() && file.getName().equals("PrivateFile")){
//read content of specific file from ftp
inputStream = new DataInputStream(client.retrieveFileStream(retrieveFromFTPFolder+file.getName()));
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//save file from ftp to local directory
File fileToWrite = new File(saveToLocalFolder+file.getName());
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileToWrite)));
while ((strLine = bufferedReader.readLine()) != null) {
out.writeBytes(strLine);
out.writeBytes("\n");
}
out.close();
//check for reply code of ftp server if required then relogin
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
System.err.println("FTP server refused connection.");
client.disconnect();
client = new FTPClient();
client.connect(ftpAddress);
client.login(userName, passWord);
}
//save file from local to remote ftp file server
File fileToUpload = new File(saveToLocalFolder + file.getName());
FileInputStream fileInputStream = new FileInputStream(fileToUpload);
client.storeFile(saveToFtpFolder + file.getName(), fileInputStream);
break;
}
}
}
}
catch (Exception e) {
if (client.isConnected()) {
try {
client.logout();
client.disconnect();
} catch (IOException f) {
// do nothing
}
}
}
finally{
if (client.isConnected())
{
try
{
client.logout();
client.disconnect();
}
catch (IOException f)
{
}
}
}
}
public static void main(String[] args) {
FtpConnect();
}
}
No comments:
Post a Comment