class="java" name="code">
package com.util;
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.ArrayList;
import java.util.List;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.log4j.Logger;
public class FtpUtil {
public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;
/** 系统日志对象 */
private static Logger log = Logger.getLogger(FtpUtil.class);
/** FTP client */
private FTPClient ftpClient;
/** FTP Server name/ip */
private String server;
/** FTP Server port */
private int port = 21;
/** FTP Server login user */
private String user;
/** FTP Server login password */
private String password;
/** FTP Server working path */
private String path;
/** 下载超时时间(毫秒)*/
private int downloadTimeout = 10000;
public void setDownloadTimeout(int downloadTimeout) {
this.downloadTimeout = downloadTimeout;
}
public int getDownloadTimeout() {
return downloadTimeout;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @param path the path to set
*/
public void setPath(String path) {
this.path = path;
}
/**
* @param port the port to set
*/
public void setPort(int port) {
this.port = port;
}
/**
* @param server the server to set
*/
public void setServer(String server) {
this.server = server;
}
/**
* @param user the user to set
*/
public void setUser(String user) {
this.user = user;
}
public FtpUtil() {
}
/**
* 创建ftp
* @param ftpServer
* @param ftpPort
* @param ftpUser
* @param ftpPwd
* @param ftpPath
*/
public FtpUtil(String ftpServer, int ftpPort, String ftpUser, String ftpPwd, String ftpPath) {
setServer(ftpServer);
setPort(ftpPort);
if (ftpPath != null && !"".equals(ftpPath))
setPath(ftpPath);
setUser(ftpUser);
setPassword(ftpPwd);
}
/**
* ftp连接
* @throws SocketException
* @throws IOException
*/
public void connectServer() throws SocketException, IOException {
ftpClient = new FTPClient();
if (server != null && !(server.equals("")) && port != -1){
//ftpClient.setDefaultTimeout(downloadTimeout);
ftpClient.setConnectTimeout(downloadTimeout);
log.info("Connected to " + server + ".");
ftpClient.connect(server, port);
log.info(ftpClient.getReplyCode());
ftpClient.login(user, password);
// Path is the sub-path of the FTP path
if (path != null && path.length() != 0) {
log.info("System encode :" + System.getProperty("file.encoding"));
log.info("Ftp encode :" + ftpClient.getControlEncoding());
//编码转换支持中文路径
boolean chg = ftpClient.changeWorkingDirectory(new String(path.getBytes(),ftpClient.getControlEncoding()));
log.info("change path:" + path + "[" + chg + "]");
}
//设置编码 支持中文文件下载
ftpClient.setControlEncoding(System.getProperty("file.encoding"));
}
}
// FTP.BINARY_FILE_TYPE | FTP.ASCII_FILE_TYPE
// Set transform type
public void setFileType(int fileType) throws IOException {
ftpClient.setFileType(fileType);
}
public void closeServer() throws IOException {
if (ftpClient != null && ftpClient.isConnected()) {
ftpClient.disconnect();
}
}
// =======================================================================
// == About directory =====
// The following method using relative path better.
// =======================================================================
public boolean changeDirectory(String path) throws IOException {
return ftpClient.changeWorkingDirectory(path);
}
public boolean createDirectory(String pathName) throws IOException {
return ftpClient.makeDirectory(pathName);
}
public boolean removeDirectory(String path) throws IOException {
return ftpClient.removeDirectory(path);
}
// delete all subDirectory and files.
public boolean removeDirectory(String path, boolean isAll)
throws IOException {
if (!isAll) {
return removeDirectory(path);
}
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
if (ftpFileArr == null || ftpFileArr.length == 0) {
return removeDirectory(path);
}
//
for (FTPFile ftpFile : ftpFileArr) {
String name = ftpFile.getName();
if (ftpFile.isDirectory()) {
log.info("* [sD]Delete subPath [" + path + "/" + name
+ "]");
removeDirectory(path + "/" + name, true);
} else if (ftpFile.isFile()) {
log.info("* [sF]Delete file [" + path + "/" + name
+ "]");
deleteFile(path + "/" + name);
} else if (ftpFile.isSymbolicLink()) {
} else if (ftpFile.isUnknown()) {
}
}
return ftpClient.removeDirectory(path);
}
// Check the path is exist; exist return true, else false.
public boolean existDirectory(String path) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
for (FTPFile ftpFile : ftpFileArr) {
if (ftpFile.isDirectory()
&& ftpFile.getName().equalsIgnoreCase(path)) {
flag = true;
break;
}
}
return flag;
}
// =======================================================================
// == About file =====
// Download and Upload file using
// ftpUtil.setFileType(FtpUtil.BINARY_FILE_TYPE) better!
// =======================================================================
// #1. list & delete operation
// Not contains directory
public List<String> getFileList(String path) throws IOException {
// listFiles return contains directory and file, it's FTPFile instance
// listNames() contains directory, so using following to filer
// directory.
// String[] fileNameArr = ftpClient.listNames(path);
FTPFile[] ftpFiles = ftpClient.listFiles(path);
List<String> retList = new ArrayList<String>();
if (ftpFiles == null || ftpFiles.length == 0) {
return retList;
}
for (FTPFile ftpFile : ftpFiles) {
if (ftpFile.isFile()) {
retList.add(ftpFile.getName());
}
}
return retList;
}
public boolean deleteFile(String pathName) throws IOException {
return ftpClient.deleteFile(pathName);
}
// #2. upload to ftp server
// InputStream <------> byte[] simple and See API
public boolean uploadFile(String fileName, String newName)
throws IOException {
boolean flag = false;
InputStream iStream = null;
try {
iStream = new FileInputStream(fileName);
flag = ftpClient.storeFile(newName, iStream);
} catch (IOException e) {
flag = false;
return flag;
} finally {
if (iStream != null) {
iStream.close();
}
}
return flag;
}
public boolean uploadFile(String fileName) throws IOException {
return uploadFile(fileName, fileName);
}
public boolean uploadFile(InputStream iStream, String newName)
throws IOException {
boolean flag = false;
try {
// can execute [OutputStream storeFileStream(String remote)]
// Above method return's value is the local file stream.
flag = ftpClient.storeFile(newName, iStream);
} catch (IOException e) {
flag = false;
return flag;
} finally {
if (iStream != null) {
iStream.close();
}
}
return flag;
}
// #3. Down load
public boolean download(String remoteFileName, String localFileName)
throws IOException {
boolean flag = false;
File outfile = new File(localFileName);
OutputStream oStream = null;
try {
oStream = new FileOutputStream(outfile);
flag = ftpClient.retrieveFile(remoteFileName, oStream);
} catch (IOException e) {
flag = false;
return flag;
} finally {
oStream.close();
}
return flag;
}
public InputStream downFile(String sourceFileName) throws IOException {
return ftpClient.retrieveFileStream(sourceFileName);
}
public Logger getLog() {
return log;
}
public void setLog(Logger log) {
this.log = log;
}
}
工具类的使用
package com.ftp;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.log4j.Logger;
import com.util.FtpUtil;
public class FTPTest {
/** 系统日志对象 */
private static Logger logger = Logger.getLogger(FTPTest.class);
public static void main(String[] args) throws IOException {
//FTP下载文件
new FTPTest().downFile("B_100018_DTL01_20170614_0001.txt.gz","D:"+File.separator+"ftp");
//FTP上传文件
new FTPTest().uploadFile("F_100019_DTL02_20170615_0002.txt.gz","D:"+File.separator+"ftp");
}
/**
* FTP下载文件
* @param fileName 文件名称
* @param localPath 本地地址
* @throws IOException
*/
public void downFile(String fileName,String localPath) throws IOException{
String ftpServer ="172.16.10.55"; //properties.getProperty(FundChannelKey.FTP_SERVER);
int ftpPort =21; //Integer.parseInt(properties.getProperty(FundChannelKey.FTP_PORT));
String ftpUser = "100018";//properties.getProperty(FundChannelKey.FTP_USER);
String ftpPwd ="100018"; //properties.getProperty(FundChannelKey.FTP_PWD);
String ftpPath ="/DTL01"; //properties.getProperty(FundChannelKey.FTP_PATH);
boolean isDown = false;
FtpUtil ftp = new FtpUtil(ftpServer, ftpPort, ftpUser, ftpPwd, ftpPath);
//ftp.setLog(logger);
try {
//连接ftp服务器
System.out.println("开始连接ftp服务器" + ftpServer + ":" + ftpPort + ftpPath);
ftp.connectServer();
System.out.println("ftp服务器连接成功");
//获取文件列表
List<String> fileList = ftp.getFileList(null);
if (fileList != null && fileList.size() > 0) {
System.out.println("共有文件[" + fileList.size() + "]个");
File localPathDir= new File(localPath);
if(!localPathDir.exists()){
localPathDir.mkdirs();
}
for (String file : fileList) {
System.out.println("比较文件[" + file + "][" + fileName + "],匹配[" + file.equals(fileName) + "]");
if (file.equals(fileName)) {
ftp.download(file, localPath + File.separator + fileName);
//从服务器删除文件
//ftp.deleteFile(file);
isDown = true;
System.out.println("[ftp下载执行完成]");
break;
}
}
}
ftp.closeServer();
} catch (Exception e) {
System.out.println("[对账文件下载失败][FTP异常]"+ e);
throw e;
} finally {
ftp.closeServer();
System.out.println("[FTP关闭]");
}
}
/**
* FTP上传文件
* @param fileName 文件名称
* @param localPath 本地地址
* @throws IOException
*/
public void uploadFile(String fileName,String localPath) throws IOException{
String ftpServer ="172.16.10.55"; //properties.getProperty(FundChannelKey.FTP_SERVER);
int ftpPort =21; //Integer.parseInt(properties.getProperty(FundChannelKey.FTP_PORT));
String ftpUser = "100019";//properties.getProperty(FundChannelKey.FTP_USER);
String ftpPwd ="100019"; //properties.getProperty(FundChannelKey.FTP_PWD);
String ftpPath ="/"; //properties.getProperty(FundChannelKey.FTP_PATH);
String uploadDir="STL01";
FtpUtil ftp = new FtpUtil(ftpServer, ftpPort, ftpUser, ftpPwd, ftpPath);
//ftp.setLog(logger);
try {
//连接ftp服务器
System.out.println("开始连接ftp服务器" + ftpServer + ":" + ftpPort + ftpPath);
ftp.connectServer();
System.out.println("ftp服务器连接成功");
//创建并改变目录位置
ftp.createDirectory(uploadDir);
ftp.changeDirectory(uploadDir);
//上传文件
if(ftp.uploadFile(localPath+File.separator+fileName,fileName))
System.out.println("上传文件成功,文件本地地址:"+localPath+File.separator+fileName);
ftp.closeServer();
} catch (Exception e) {
System.out.println("[对账文件下载失败][FTP异常]"+ e);
throw e;
} finally {
ftp.closeServer();
System.out.println("[FTP关闭]");
}
}
}