public class FtpUtil {
private static Logger logger = Logger.getLogger(FtpUtil.class);
private static FTPClient CLIENT = null;
private static String LOCAL_CHARSET = "GBK";
private static String SERVER_CHARSET = "ISO-8859-1";
private static final String FTP_HOST = ConfigureUtil.getString("ftp.host");
private static final int FTP_PORT = ConfigureUtil.getInt("ftp.post");
private static final String FTP_USERNAME = ConfigureUtil.getString("ftp.username");
private static final String FTP_PASSWORD = ConfigureUtil.getString("ftp.password");
/**
* 获取FTPClient对象
*
* @param ftpHost FTP主机服务器
* @param ftpPassword FTP 登录密码
* @param ftpUserName FTP登录用户名
* @param ftpPort FTP端口 默认为21
* @return
*/
public static void getFTPClient() {
try {
CLIENT = new FTPClient();
CLIENT.connect(FTP_HOST, FTP_PORT);// 连接FTP服务器
CLIENT.login(FTP_USERNAME, FTP_PASSWORD);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(CLIENT.getReplyCode())) {
logger.error("未连接到FTP,用户名或密码错误。");
CLIENT.disconnect();
} else {
logger.info("FTP连接成功。");
int reply;
reply = CLIENT.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
CLIENT.disconnect();
}
if (FTPReply.isPositiveCompletion(CLIENT.sendCommand(
"OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
LOCAL_CHARSET = "UTF-8";
}
CLIENT.setControlEncoding(LOCAL_CHARSET);
CLIENT.setFileType(FTP.BINARY_FILE_TYPE);
CLIENT.enterLocalPassiveMode();
}
} catch (SocketException e) {
e.printStackTrace();
logger.error("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
logger.error("FTP的端口错误,请正确配置。");
}
}
/*
* 从FTP服务器下载文件
*
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
* @param localPath 下载到本地的位置 格式:H:/download
* @param fileName 文件名称
*/
@SuppressWarnings("static-access")
public static boolean downloadFtpFile(String ftpPath, String localPath,String fileName) {
boolean isAppend = false;
try {
getFTPClient();
CLIENT.setFileTransferMode(CLIENT.STREAM_TRANSFER_MODE);
ftpPath = new String(ftpPath.getBytes(LOCAL_CHARSET),SERVER_CHARSET);
CLIENT.changeWorkingDirectory(ftpPath);
File localFile = new File(localPath + File.separatorChar + fileName);
if(!new File(localPath).exists()){
new File(localPath).mkdirs();
}
if(localFile.exists()){
return true;
}
OutputStream os = new FileOutputStream(localFile);
isAppend = CLIENT.retrieveFile(new String(fileName.getBytes(LOCAL_CHARSET),SERVER_CHARSET), os);
os.close();
CLIENT.logout();
return isAppend;
} catch (FileNotFoundException e) {
logger.error("没有找到" + ftpPath + "文件");
e.printStackTrace();
} catch (SocketException e) {
logger.error("连接FTP失败.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
logger.error("文件读取错误。");
e.printStackTrace();
}
return isAppend;
}
/**
* Description: 向FTP服务器上传文件
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
* @param fileName ftp文件名称
* @param input 文件流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String ftpPath,String fileName,InputStream input) {
boolean success = false;
try {
getFTPClient();
ftpPath = new String(ftpPath.getBytes(LOCAL_CHARSET),SERVER_CHARSET);
CLIENT.makeDirectory(ftpPath);
CLIENT.changeWorkingDirectory(ftpPath);
fileName = new String(fileName.getBytes(LOCAL_CHARSET),SERVER_CHARSET);
success = CLIENT.storeFile(fileName, input);
logger.info("上传成功");
input.close();
CLIENT.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (CLIENT.isConnected()) {
try {
CLIENT.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* Description: 向FTP服务器上传图片
* @param ftpPath FTP服务器中文件所在路径,把路径按照级别放到数组中
* @param fileName ftp文件名称
* @param input 文件流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String[] ftpPath,String fileName,InputStream input) {
boolean success = false;
try {
getFTPClient();
String path = "";
for(int i=0;ipara){
boolean isAppend = false;
try {
String path = para.get("path")+"";
String name = para.get("name")+"";
getFTPClient();
path = new String(path.getBytes(LOCAL_CHARSET),SERVER_CHARSET);
CLIENT.changeWorkingDirectory(path);
name = new String(name.getBytes(LOCAL_CHARSET),SERVER_CHARSET);
CLIENT.dele(name);
logger.info("删除成功");
CLIENT.logout();
isAppend = true;
} catch (Exception e) {
e.printStackTrace();
}finally {
if (CLIENT.isConnected()) {
try {
CLIENT.disconnect();
} catch (IOException ioe) {
}
}
}
return isAppend;
}
/**
* @MethodName deleteFolder
* @Description 删除ftp文件夹
* @param para
* @return
*
*/
public static boolean deleteFolder(String folder){
boolean isAppend = false;
try {
getFTPClient();
folder = new String(folder.getBytes(LOCAL_CHARSET),SERVER_CHARSET);
isAppend = CLIENT.removeDirectory(folder);
logger.info("删除成功");
CLIENT.logout();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (CLIENT.isConnected()) {
try {
CLIENT.disconnect();
} catch (IOException ioe) {
}
}
}
return isAppend;
}
/**
* @MethodName reName
* @Description 修改文件名称
* @param para
* @return
*
*/
public static boolean reName(Mappara){
boolean isAppend = false;
try {
String path = para.get("path")+"";
String name = para.get("name")+"";
getFTPClient();
path = new String(path.getBytes(LOCAL_CHARSET),SERVER_CHARSET);
name = new String(name.getBytes(LOCAL_CHARSET),SERVER_CHARSET);
isAppend = CLIENT.rename(path, name);
logger.info("修改文件夹共成功");
CLIENT.logout();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (CLIENT.isConnected()) {
try {
CLIENT.disconnect();
} catch (IOException ioe) {
}
}
}
return isAppend;
}
}