public class FtpUtil {
/**
* 日志记录
*/
private static Logger logger = LoggerFactory.getLogger(FtpUtil.class);
/**
*
* 功能描述: 连接ftp服务器
*
*/
private static FTPClient connect(FtpServerInfo info) {
FTPClient ftp = new FTPClient();
try {
ftp.setRemoteHost(info.getHost());
ftp.setRemotePort(info.getPort());
ftp.setControlEncoding("UTF-8");
ftp.connect();
ftp.login(info.getUsername(), info.getPassword());
ftp.setType(FTPTransferType.BINARY);
} catch (FTPException e) {
logger.error("连接 FTP服务器 异常=====" + e.getMessage());
} catch (IOException e) {
logger.error("连接 FTP服务器IO异常=====" + e.getMessage());
}
return ftp;
}
/**
*
* 从服务器得到json文件内容
*
*/
public static String getJsonFile(FtpServerInfo info) {
FTPClient ftp = null;
try {
ftp = connect(info);
if (ftp == null) {
throw new RuntimeException("ftpClient object is null!");
}
String jsonFile = File.separator + info.getRootPath() + File.separator + "file.json";
if (ftp.existsFile(jsonFile)) {
byte[] bytes =
ftp.get(jsonFile);
return new String(bytes, Charset.forName("UTF-8"));
} else {
return "";
}
} catch (Exception e) {
logger.error("得到json文件异常===============" + e.getMessage());
} finally {
try {
if (ftp != null) {
ftp.quit();
}
} catch (IOException e1) {
} catch (FTPException e1) {
}
}
}
/**
*
* 功能描述: 重新上传json文件
*
*/
public static void putJsonFile(FtpServerInfo info, String jsonContent) {
FTPClient ftp = null;
try {
ftp = connect(info);
if (ftp == null) {
throw new RuntimeException("ftpClient object is null!");
}
String jsonFile = File.separator + info.getRootPath() + File.separator + "file.json";
ftp.put(jsonContent.getBytes(Charset.forName("UTF-8")), jsonFile);
} catch (Exception e) {
logger.error("上传 json文件异常===============" + e.getMessage());
} finally {
try {
if (ftp != null) {
ftp.quit();
}
} catch (IOException e1) {
} catch (FTPException e1) {
}
}
}
/**
*
* 功能描述: 根据自提柜代码删除 ftp服务器相应目录下的文件
*
*/
public static void deleteFile(String lockerCode, FtpServerInfo info) {
FTPClient ftp = null;
try {
ftp = connect(info);
if (ftp == null) {
throw new RuntimeException("ftpClient object is null!");
}
deleteFolder(ftp, File.separator + info.getRootPath() + File.separator + code);
} catch (Exception e) {
logger.error("删除文件异常===============" + e.getMessage());
} finally {
try {
if (ftp != null) {
ftp.quit();
}
} catch (IOException e1) {
} catch (FTPException e1) {
}
}
}
/**
*
* 功能描述: 上传 文件
*/
public static void uploadFile(InputStream is, String code, String fileName, FtpServerInfo info) {
FTPClient ftp = null;
String rootPath = null;
String codePath = null;
try {
ftp = connect(info);
if (ftp == null) {
throw new RuntimeException("ftpClient object is null!");
}
// 防止根目录不存在 先建立
rootPath = File.separator + info.getRootPath();
if (!ftp.existsDirectory(rootPath)) {
ftp.mkdir(rootPath);
}
// 若存在 则先删除对应的目录
codePath = rootPath + File.separator + code;
if (ftp.existsDirectory(lockerCodePath)) {
deleteFolder(ftp, codePath);
}
// 新建目录
ftp.mkdir(codePath);
// 上传文件
ftp.put(is, codePath+ File.separator + fileName);
} catch (IOException e) {
logger.error("上传文件至 FTP服务器连接 异常=====" + e.getMessage());
} catch (FTPException e) {
logger.error("上传文件至 FTP服务器连接 异常=====" + e.getMessage());
} finally {
try {
if (ftp != null) {
ftp.quit();
}
} catch (IOException e1) {
} catch (FTPException e1) {
}
}
}
/**
*
* 功能描述:递归删除目录下文件及子文件夹
*
*/
private static void deleteFolder(FTPClient ftpClient, String dirPath) {
try {
// 切换到根目录
ftpClient.chdir(File.separator);
FTPFile[] ftpFiles = ftpClient.dirDetails(dirPath);
if (ftpFiles == null) {
return;
}
for (FTPFile file : ftpFiles) {
if (file.isDir()) {
deleteFolder(ftpClient, dirPath + File.separator + file.getName());
}
if (file.isFile()) {
ftpClient.delete(dirPath + File.separator + file.getName());
}
}
// 回到 父目录 删除
ftpClient.cdup();
ftpClient.rmdir(dirPath);
} catch (IOException e) {
} catch (FTPException e) {
} catch (ParseException e) {
}
}
}