package com.cherri.batch.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import com.cherri.batch.exception.FTPException;
import com.cherri.batch.log.Log;
public class FTPUtils {
private static FTPClient connect(String serverIp, String port, String account, String password)
throws FTPException {
try {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(serverIp, Integer.valueOf(port));
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
throw new FTPException("FTP Connect to server fail");
}
if (!ftpClient.login(account, password)) {
throw new FTPException("FTP Login fail");
}
ftpClient.enterLocalPassiveMode();
return ftpClient;
} catch (NumberFormatException | IOException e) {
Log.exception(e);
throw new FTPException("FTP Connect exception");
}
}
public static boolean deleteFile(String serverIp, String port, String account, String password, String fileName) {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
try {
ftpClient = connect(serverIp, port, account, password);
if (!ftpClient.deleteFile(fileName)) {
Log.error("FTP delete file fail with fileName: {}", fileName);
return false;
}
return true;
} catch (FTPException e) {
Log.error(e.getMessage() + " with fileName: {}", fileName);
return false;
} catch (Exception e) {
Log.exception(e);
return false;
} finally {
try {
if (fis != null) {
fis.close();
}
ftpClient.disconnect();
} catch (Exception e) {
Log.exception(e);
}
}
}
public static boolean uploadFile(String serverIp, String port, String account, String password, String remotePath,
File file) {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
try {
ftpClient = connect(serverIp, port, account, password);
if (!"/".equals(remotePath) && !ftpClient.changeWorkingDirectory(remotePath)) {
Log.error("FTP remotePath not exist with fileName: {}", file.getName());
return false;
}
fis = new FileInputStream(file);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
if (!ftpClient.storeFile(file.getName(), fis)) {
Log.error("FTP store file fail with fileName: {}", file.getName());
return false;
}
return true;
} catch (FTPException e) {
Log.error(e.getMessage() + " with fileName: {}", file.getName());
return false;
} catch (Exception e) {
Log.exception(e);
return false;
} finally {
try {
if (fis != null) {
fis.close();
}
ftpClient.disconnect();
} catch (Exception e) {
Log.exception(e);
}
}
}
public static File downloadFile(String serverIp, String port, String account, String password, String remotePath,
String localPath, String fileName) {
FTPClient ftpClient = new FTPClient();
FileOutputStream fos = null;
try {
ftpClient = connect(serverIp, port, account, password);
File downloadFileDirectory = new File(localPath);
if (!downloadFileDirectory.exists() && !downloadFileDirectory.mkdirs()) {
Log.error("FTP localPath not exist fileName: {}", fileName);
return null;
}
File downloadFile = new File(localPath + fileName);
fos = new FileOutputStream(downloadFile);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
if (!ftpClient.retrieveFile(remotePath + "/" + fileName, fos)) {
Log.error("FTP retrieve file fail with fileName: {}", fileName);
return null;
}
return downloadFile;
} catch (FTPException e) {
Log.error(e.getMessage() + " with fileName: {}", fileName);
return null;
} catch (Exception e) {
Log.exception(e);
return null;
} finally {
try {
if (fos != null) {
fos.close();
}
ftpClient.disconnect();
} catch (Exception e) {
Log.exception(e);
}
}
}
}