/**
*
*/
package com.cv.ftp.connect;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
/**
* @author Chandra Vardhan
*
*/
public class FTPConnect {
static Properties readPropertiesFile = new Properties();
static {
readPropertiesFile = readPropertiesFile();
}
private static final Logger LOGGER = Logger.getLogger(FTPConnect.class);
private FTPClient ftpClient = null;
private static String sourceDirectory = null;
private static String destinationFtpAddress = null;
private static String destinationFtpUserId = null;
private static String destinationFtpPassword = null;
private static String destinationDirectory = null;
/*
* (non-Javadoc)
*
* @see com.cv.ftp.read.FTPService#sendFileViaFTP(java.lang.String)
*/
public void sendFileViaFTP(String fileName) {
setValues(readPropertiesFile);
LOGGER.info("uploading file to FTP...");
ftpClient = getFtpClient();
InputStream file = null;
try {
file = new FileInputStream(fileName);
String formatedFileName = new File(fileName).getName();
boolean isFileStored = ftpClient.storeFile(formatedFileName, file);
LOGGER.info(String.format("store file to ftp %s:%s", formatedFileName, isFileStored));
LOGGER.info("file upload success...");
} catch (IOException e) {
LOGGER.error("An IOException has occured..." + e);
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
file.close();
} catch (IOException ioe) {
LOGGER.error("An IOException has occured..." + ioe);
ioe.printStackTrace();
}
}
}
}
private static void setValues(Properties readPropertiesFile2) {
if (readPropertiesFile2 != null) {
sourceDirectory = readPropertiesFile2.getProperty("ftp.source.fileLocation");
destinationFtpAddress = readPropertiesFile2.getProperty("destination.ftp.address");
destinationFtpUserId = readPropertiesFile2.getProperty("destination.ftp.user");
destinationFtpPassword = readPropertiesFile2.getProperty("destination.ftp.pass");
destinationDirectory = readPropertiesFile2.getProperty("backup.fileLocation");
}
}
/*
* (non-Javadoc)
*
* @see com.cv.ftp.read.FTPService#getFileViaFTP()
*/
public String getFileViaFTP() {
String name = null;
setValues(readPropertiesFile);
ftpClient = getFtpClient();
OutputStream outputFile = null;
try {
boolean isDirChanged = ftpClient.changeWorkingDirectory(sourceDirectory);
String[] fileNames = ftpClient.listNames();
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++) {
name = fileNames[i];
if (name != null) {
outputFile = new FileOutputStream(destinationDirectory + name);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.retrieveFile(name, outputFile);
outputFile.flush();
outputFile.close();
ftpClient.deleteFile(name);
}
}
}
} catch (IOException e) {
LOGGER.error("An IOException has occured..." + e);
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
if (outputFile != null) {
outputFile.close();
}
} catch (IOException ioe) {
LOGGER.error("An IOException has occured..." + ioe);
ioe.printStackTrace();
}
}
}
return null;
}
/**
* @return the ftpClient
*/
public FTPClient getFtpClient() {
ftpClient = new FTPClient();
FTPClientConfig config = new FTPClientConfig();
boolean error = false;
int reply;
try {
ftpClient.connect(destinationFtpAddress);
ftpClient.user(destinationFtpUserId);
ftpClient.pass(destinationFtpPassword);
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
LOGGER.error("FTP server refused connection.");
}
ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
// Use passive mode as default because most of us are
// behind firewalls these days.
// ftpClient.enterLocalPassiveMode();
} catch (SocketException e) {
LOGGER.error("An SocketException has occured..." + e);
e.printStackTrace();
} catch (IOException e) {
LOGGER.error("An IOException has occured..." + e);
e.printStackTrace();
}
return ftpClient;
}
/**
* @param ftpClient
* the ftpClient to set
*/
public void setFtpClient(FTPClient ftpClient) {
this.ftpClient = ftpClient;
}
public String getDestinationFtpAddress() {
return destinationFtpAddress;
}
public void setDestinationFtpAddress(String destinationFtpAddress) {
FTPConnect.destinationFtpAddress = destinationFtpAddress;
}
public String getDestinationFtpUserId() {
return destinationFtpUserId;
}
public void setDestinationFtpUserId(String destinationFtpUserId) {
FTPConnect.destinationFtpUserId = destinationFtpUserId;
}
public String getDestinationFtpPassword() {
return destinationFtpPassword;
}
public void setDestinationFtpPassword(String destinationFtpPassword) {
FTPConnect.destinationFtpPassword = destinationFtpPassword;
}
public String getSourceDirectory() {
return sourceDirectory;
}
public void setSourceDirectory(String sourceDirectory) {
FTPConnect.sourceDirectory = sourceDirectory;
}
public String getDestinationDirectory() {
return destinationDirectory;
}
public void setDestinationDirectory(String destinationDirectory) {
FTPConnect.destinationDirectory = destinationDirectory;
}
private static Properties readPropertiesFile() {
Properties props = new Properties();
try {
props.load(FTPConnect.class.getClassLoader().getResourceAsStream("input.properties"));
} catch (Exception e) {
System.err.println("Problem while reading properties file!!!");
}
return props;
}
public static void main(String[] args) {
FTPConnect ftpService = new FTPConnect();
ftpService.getFileViaFTP();
}
public static void moveFile(String srcDir, String destDir) {
File sourceFile = new File(srcDir);
File destFile = new File(destDir);
try {
FileUtils.copyFile(sourceFile, destFile);
FileUtils.forceDelete(sourceFile);
} catch (IOException e) {
e.printStackTrace();
}
}
} |
No comments:
Post a Comment