/* @(#)
*
* Project:Clear
*
* Modify Information:
* =============================================================================
* Author Date Description
* ------------ ---------- ---------------------------------------------------
* lihj 2015年7月17日 first release
*
*
* Copyright Notice:
* =============================================================================
* Copyright 2015 Quantdo.com.cn All rights reserved.
*
*
* Warning:
* =============================================================================
*
*/
package com.quantdo.orgClear.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import com.dev.framework.core.configure.ConfigureProperties;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import javax.inject.Inject;
/**
*
* <br>
* 创建日期:2015年7月17日 下午3:32:30 <br>
* <b>Copyright 2015 上海量投网络科技有限公司 All Rights Reserved</b>
*
* @author 李会军
* @since 1.0
* @version 1.0
*/
public class FtpUtil {
private final static Logger logger = Logger.getLogger(FtpUtil.class);
private String server;
private String username;
private String password;
private FTPClient ftp;
@Inject
private ConfigureProperties config;
public static final String JS_FILE_SEPARATOR = "/";
/**
* true:binary模式下载 false:ascii模式下载
*/
private boolean binaryTransfer = true;
/**
* @param server
* ftp服务器地址
* @param username
* ftp服务器登陆用户
* @param password
* ftp用户密码
*/
public FtpUtil(String server, String username, String password) {
this.server = server;
this.username = username;
this.password = password;
this.ftp = new FTPClient();
// // 打印FTP命令
// if(Configuration.PrintFTPCommandLog){
// ftp.addProtocolCommandListener(new PrintCommandListener());
// }
}
/**
* 连接FTP服务器
*
* @throws Exception
*/
public void connect() throws Exception {
try {
ftp.connect(server);
// 连接后检测返回码来校验连接是否成功
int reply = ftp.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
if (ftp.login(username, password)) {
ftp.enterLocalPassiveMode(); // 设置为passive模式
}
} else {
ftp.disconnect();
logger.error("连接FTP服务器失败:FTP服务器拒绝连接.");
throw new Exception("连接FTP服务器失败:FTP服务器拒绝连接.");
}
} catch (IOException e) {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
logger.error("连接FTP服务器失败:关闭FTP服务器失败.");
}
}
e.printStackTrace();
logger.error("连接FTP服务器失败:无法连接服务器.", e);
throw new Exception("连接FTP服务器失败:无法连接服务器.", e);
}
}
/**
* 列出远程目录下所有的文件
*
* @param remotePath
* 远程目录名
* @return 远程目录下所有文件名的列表,目录不存在或者目录下没有文件时返回0长度的数组
*/
public String[] listNames(String remotePath) throws Exception {
String[] fileNames = null;
try {
FTPFile[] remotefiles = ftp.listFiles(remotePath);
fileNames = new String[remotefiles.length];
for (int i = 0; i < remotefiles.length; i++) {
fileNames[i] = remotefiles[i].getName();
}
} catch (IOException e) {
e.printStackTrace();
logger.error("无法列出FTP服务器文件列表.", e);
throw new Exception("无法列出FTP服务器文件列表.", e);
}
return fileNames;
}
/**
* 列出远程目录下所有的文件
*
* @param remotePath
* 远程目录名
* @return 远程目录下所有文件名的列表,目录不存在或者目录下没有文件时返回0长度的数组
*/
public String[] listNames2(String remotePath) throws Exception {
List<String> fileNameList = new ArrayList<String>();
String[] fileNames = null;
try {
FTPFile[] remotefiles = ftp.listFiles(remotePath);
for (int i = 0; i < remotefiles.length; i++) {
if (remotefiles[i].isFile()) {
fileNameList.add(remotefiles[i].getName());
}
}
fileNames = new String[fileNameList.size()];
if (fileNameList.size() < 1) {
return null;
} else {
for (int i = 0; i < fileNameList.size(); i++) {
fileNames[i] = (String) fileNameList.get(i);
}
}
} catch (IOException e) {
e.printStackTrace();
logger.error("无法列出FTP服务器文件列表.", e);
throw new Exception("无法列出FTP服务器文件列表.", e);
}
return fileNames;
}
/**
* =======================上传文件========================
*/
/**
* 上传多个文件到远程目录中
*
* @param localPath
* 本地文件所在路径(路径:path)
* @param remotePath
* 目标文件所在路径(路径:path)
* @param fileNames
* 本地文件名数组(文件名:filename)数组
* @return
*/
public boolean[] putFilesToPath(String localPath, String remotePath, String[] fileNames) {
String[] localFullPaths = new String[fileNames.length];
String[] remoteFullPaths = new String[fileNames.length];
for (int i = 0; i < fileNames.length; i++) {
localFullPaths[i] = localPath + JS_FILE_SEPARATOR + fileNames[i];
remoteFullPaths[i] = remotePath + JS_FILE_SEPARATOR + fileNames[i];
}
return this.putFilesToFiles(localFullPaths, remoteFullPaths);
}
/**
* 上传多个文件到远程路径中
*
* @param localFullPaths
* 本地文件名数组(完整全路径:path+filename)
* @param remoteFullPaths
* 目标路径数组(完整全路径:path+filename)
* @return
*/
public boolean[] putFilesToFiles(String[] localFullPaths, String[] remoteFullPaths) {
// 初始化返回结构数组
boolean[] result = new boolean[localFullPaths.length];
for (int j = 0; j < result.length; j++) {
result[j] = false;
}
//
for (int i = 0; i < localFullPaths.length; i++) {
String localFullPath = localFullPaths[i];
String remoteFullPath = remoteFullPaths[i];
result[i] = this.putFileToFile(localFullPath, remoteFullPath);
}
return result;
}
/**
* 上传一个文件到远程指定路径中
*
* @param localFullPath
* 本地文件名(完整全路径:path+filename)
* @param remoteFullPath
* 目标路径(完整全路径:path+filename)
* @return 成功时,返回true,失败返回false
*/
public boolean putFileToFile(String localFullPath, String remoteFullPath) {
return this.put(localFullPath, remoteFullPath);
}
/**
* 上传一个本地文件到远程指定文件
*
* @param localFullFile
* 本地文件名(包括完整路径)
* @param remoteFullFile
* 远程文件名(包括完整路径)
* @return 成功时,返回true,失败返回false
*/
private boolean put(String localFullFile, String remoteFullFile) {
boolean result = false;
InputStream input = null;
try {
// 设置文件传输类型
if (binaryTransfer) {
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
} else {
ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
}
// 处理传输
input = new FileInputStream(localFullFile); // 将本地文件读成数据流
result = ftp.storeFile(remoteFullFile, input);