com.enterprisedt.net.ftp.FTPClient FTP文件上传操作
2013-12-25 11:48
567 查看
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) {
}
}
}
/**
* 日志记录
*/
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) {
}
}
}
相关文章推荐
- com.enterprisedt.net.ftp.FTPClient下载和上传
- FTPClient--FTP操作帮助类,上传下载,文件,目录操作
- sun.net.ftp.FtpClient 操作FTP服务器上的文件和目录
- org.apache.commons.net.ftp.FTPClient文件上传下载,中文乱码
- Apache FTPClient操作文件上传下载及公共类
- 关于 com.enterprisedt.net.ftp.FTPClient 的使用方法(转)
- 教大家用FTPClient对ftp文件进行上传下载等操作
- FTP文件的上传和下载---org.apache.commons.net.ftp.FTPClient
- C# FTPClientHelper共公类 实现文件上传,目录操作,下载等动作
- sun.net.ftp.FtpClient下载、上传文件
- FTP文件的上传和下载---org.apache.commons.net.ftp.FTPClient
- 用FTPClient对ftp文件进行上传下载等操作
- Microsoft .NET Framework 2.0对文件传输协议(FTP)操作(上传,下载,新建,删除,FTP间传送文件等)实现汇总1
- org.apache.commons.net.ftp.FTPClient上传、下载、修改文件等功能
- C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
- .Net 2.0对文件传输协议(FTP)操作(上传,下载,新建,删除,FTP间传送文件等)
- C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
- .Net 2.0对文件传输协议(FTP)操作(上传,下载,新建,删除,FTP间传送文件等)
- [FTP] FTPClient--FTP操作帮助类,上传下载,文件,目录操作 (转载)
- org.apache.commons.net.ftp.FTPClient上传文件大小改变的解决方法