package zzvcom.cms.ccm.ftp;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FileTransferClient;
import com.enterprisedt.net.ftp.WriteMode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import zzvcom.cms.ccm.commons.StringTookit;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
/**
* FTP增强工具
*
* @author leizhimin 2008-12-13 16:13:01
*/
public class UltraFTPClient extends FileTransferClient {
private static Log log = LogFactory.getLog(UltraFTPClient.class);
public UltraFTPClient() {
}
/**
* 下载文件(夹),在本地保持FTP上的目录结构
*
* @param localFolderPath 本地存放文件夹
* @param remotePath 远程文件(夹)路径
* @param remoteSubPath 远程文件存放相对根目录
* @throws FTPException
* @throws IOException
*/
public void ftpDownload(final String localFolderPath, final String remotePath, String remoteSubPath) throws FTPException, IOException, ParseException {
if (isDir(remoteSubPath)) {
String localPath = localFolderPath + StringTookit.getRelativeRootPath(remoteSubPath, StringTookit.getParentPath(remotePath));
if (!new File(localPath).exists())
new File(localPath).mkdirs();
String[] x = directoryNameList(remoteSubPath, false);
for (String fname : x) {
String rmFilePath =StringTookit.formatPath(remoteSubPath + "/" + fname);
if (isDir(rmFilePath)) {
ftpDownload(localFolderPath, remotePath, rmFilePath);
} else {
String _localPath = localFolderPath + "/" +
StringTookit.getRelativeRootPath(rmFilePath, StringTookit.getParentPath(remotePath));
// downloadFile(_localPath, rmFilePath, WriteMode.OVERWRITE);
downloadFile(_localPath, rmFilePath);
}
}
} else if (isFile(remotePath)) {
String localPath = localFolderPath + StringTookit.getRelativeRootPath(remoteSubPath, remotePath);
// downloadFile(localPath, remoteSubPath, WriteMode.OVERWRITE);
downloadFile(localPath, remoteSubPath);
} else {
log.error("所下载的文件或文件夹不存在,请检查!");
}
log.info("FTP下载从服务器上的" + remoteSubPath + "下载到本地" + localFolderPath + "结束!");
}
/**
* 上传文件(夹),在FTP上保持本地的目录结构
*
* @param localFile 本地文件
* @param localFilePath 本地文件的路径
* @param remoteSubPath 远程文件存放相对根目录
* @throws IOException
* @throws FTPException
*/
public void ftpUpload(File localFile, final String localFilePath, final String remoteSubPath) throws IOException, FTPException {
if (localFile.isDirectory()) {
for (File file : localFile.listFiles()) {
if (file.isDirectory()) {
String remotePath = StringTookit.formatPath(remoteSubPath) + StringTookit.getRelativeRootPath(file.getPath(), StringTookit.getParentPath(localFilePath));
log.info(remotePath);
if (!isExist(remotePath)) createDirectory(remotePath);
ftpUpload(file, localFilePath, remoteSubPath);
} else {
String remotePath = StringTookit.formatPath(remoteSubPath) +
StringTookit.getRelativeRootPath(file.getPath(), StringTookit.getParentPath(localFilePath));
uploadFile(file.getPath(), remotePath, WriteMode.APPEND);
}
}
} else if (localFile.isFile()) {
String remotePath = StringTookit.formatPath(remoteSubPath) +
StringTookit.getRelativeRootPath(localFile.getPath(), StringTookit.getParentPath(localFilePath));
if (!isExist(StringTookit.getParentPath(remotePath)))
createDirectory(StringTookit.getParentPath(remotePath));
System.out.println(remotePath);
uploadFile(localFile.getPath(), remotePath, WriteMode.APPEND);
}
log.info("FTP上传" + localFile.getPath() + "到" + remoteSubPath + "目录下结束!");
}
/**
* @param remotePath 远程文件(夹)路径
* @return 远程的文件(夹)资源是否存在
*/
public boolean isExist(String remotePath) {
boolean flag = true;
try {
directoryList(remotePath);
} catch (Exception e) {
flag = false;
}
return flag;
}
/**
* @param remotePath 远程文件(夹)路径
* @return 当远程资源存在且为文件时返回ture,否则返回false
*/
public boolean isFile(String remotePath) {
try {
int size = directoryList(remotePath).length;
if (size >= 0) {
if (exists(remotePath)) {
return true;
}
}
} catch (Exception e) {
}
return false;
}
/**
* @param remotePath 远程文件(夹)路径
* @return 当远程资源存在且为文件夹时返回ture,否则返回false
*/
public boolean isDir(String remotePath) {
try {
int size = directoryList(remotePath).length;
if (size >= 0) {
if (exists(remotePath)) {
return false;
} else {
return true;
}
}
} catch (Exception e) {
}
return false;
}
}
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FileTransferClient;
import com.enterprisedt.net.ftp.WriteMode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import zzvcom.cms.ccm.commons.StringTookit;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
/**
* FTP增强工具
*
* @author leizhimin 2008-12-13 16:13:01
*/
public class UltraFTPClient extends FileTransferClient {
private static Log log = LogFactory.getLog(UltraFTPClient.class);
public UltraFTPClient() {
}
/**
* 下载文件(夹),在本地保持FTP上的目录结构
*
* @param localFolderPath 本地存放文件夹
* @param remotePath 远程文件(夹)路径
* @param remoteSubPath 远程文件存放相对根目录
* @throws FTPException
* @throws IOException
*/
public void ftpDownload(final String localFolderPath, final String remotePath, String remoteSubPath) throws FTPException, IOException, ParseException {
if (isDir(remoteSubPath)) {
String localPath = localFolderPath + StringTookit.getRelativeRootPath(remoteSubPath, StringTookit.getParentPath(remotePath));
if (!new File(localPath).exists())
new File(localPath).mkdirs();
String[] x = directoryNameList(remoteSubPath, false);
for (String fname : x) {
String rmFilePath =StringTookit.formatPath(remoteSubPath + "/" + fname);
if (isDir(rmFilePath)) {
ftpDownload(localFolderPath, remotePath, rmFilePath);
} else {
String _localPath = localFolderPath + "/" +
StringTookit.getRelativeRootPath(rmFilePath, StringTookit.getParentPath(remotePath));
// downloadFile(_localPath, rmFilePath, WriteMode.OVERWRITE);
downloadFile(_localPath, rmFilePath);
}
}
} else if (isFile(remotePath)) {
String localPath = localFolderPath + StringTookit.getRelativeRootPath(remoteSubPath, remotePath);
// downloadFile(localPath, remoteSubPath, WriteMode.OVERWRITE);
downloadFile(localPath, remoteSubPath);
} else {
log.error("所下载的文件或文件夹不存在,请检查!");
}
log.info("FTP下载从服务器上的" + remoteSubPath + "下载到本地" + localFolderPath + "结束!");
}
/**
* 上传文件(夹),在FTP上保持本地的目录结构
*
* @param localFile 本地文件
* @param localFilePath 本地文件的路径
* @param remoteSubPath 远程文件存放相对根目录
* @throws IOException
* @throws FTPException
*/
public void ftpUpload(File localFile, final String localFilePath, final String remoteSubPath) throws IOException, FTPException {
if (localFile.isDirectory()) {
for (File file : localFile.listFiles()) {
if (file.isDirectory()) {
String remotePath = StringTookit.formatPath(remoteSubPath) + StringTookit.getRelativeRootPath(file.getPath(), StringTookit.getParentPath(localFilePath));
log.info(remotePath);
if (!isExist(remotePath)) createDirectory(remotePath);
ftpUpload(file, localFilePath, remoteSubPath);
} else {
String remotePath = StringTookit.formatPath(remoteSubPath) +
StringTookit.getRelativeRootPath(file.getPath(), StringTookit.getParentPath(localFilePath));
uploadFile(file.getPath(), remotePath, WriteMode.APPEND);
}
}
} else if (localFile.isFile()) {
String remotePath = StringTookit.formatPath(remoteSubPath) +
StringTookit.getRelativeRootPath(localFile.getPath(), StringTookit.getParentPath(localFilePath));
if (!isExist(StringTookit.getParentPath(remotePath)))
createDirectory(StringTookit.getParentPath(remotePath));
System.out.println(remotePath);
uploadFile(localFile.getPath(), remotePath, WriteMode.APPEND);
}
log.info("FTP上传" + localFile.getPath() + "到" + remoteSubPath + "目录下结束!");
}
/**
* @param remotePath 远程文件(夹)路径
* @return 远程的文件(夹)资源是否存在
*/
public boolean isExist(String remotePath) {
boolean flag = true;
try {
directoryList(remotePath);
} catch (Exception e) {
flag = false;
}
return flag;
}
/**
* @param remotePath 远程文件(夹)路径
* @return 当远程资源存在且为文件时返回ture,否则返回false
*/
public boolean isFile(String remotePath) {
try {
int size = directoryList(remotePath).length;
if (size >= 0) {
if (exists(remotePath)) {
return true;
}
}
} catch (Exception e) {
}
return false;
}
/**
* @param remotePath 远程文件(夹)路径
* @return 当远程资源存在且为文件夹时返回ture,否则返回false
*/
public boolean isDir(String remotePath) {
try {
int size = directoryList(remotePath).length;
if (size >= 0) {
if (exists(remotePath)) {
return false;
} else {
return true;
}
}
} catch (Exception e) {
}
return false;
}
}
2008-12-17 2:42:36 zzvcom.cms.ccm.ftp.FtpServerConfigration buildFtpClient
信息: connection ftp success!
2008-12-17 2:42:36 zzvcom.cms.ccm.ftp.UltraFTPClient ftpDownload
信息: FTP涓嬭浇浠庢湇鍔″櫒涓婄殑/ddd/a涓嬭浇鍒版湰鍦?c:/qqq缁撴潫锛?
2008-12-17 2:42:36 zzvcom.cms.ccm.ftp.UltraFTPClient ftpDownload
信息: FTP涓嬭浇浠庢湇鍔″櫒涓婄殑/ddd/b涓嬭浇鍒版湰鍦?c:/qqq缁撴潫锛?
2008-12-17 2:42:36 zzvcom.cms.ccm.ftp.UltraFTPClient ftpDownload
信息: FTP涓嬭浇浠庢湇鍔″櫒涓婄殑/ddd涓嬭浇鍒版湰鍦?c:/qqq缁撴潫锛?
信息: connection ftp success!
2008-12-17 2:42:36 zzvcom.cms.ccm.ftp.UltraFTPClient ftpDownload
信息: FTP涓嬭浇浠庢湇鍔″櫒涓婄殑/ddd/a涓嬭浇鍒版湰鍦?c:/qqq缁撴潫锛?
2008-12-17 2:42:36 zzvcom.cms.ccm.ftp.UltraFTPClient ftpDownload
信息: FTP涓嬭浇浠庢湇鍔″櫒涓婄殑/ddd/b涓嬭浇鍒版湰鍦?c:/qqq缁撴潫锛?
2008-12-17 2:42:36 zzvcom.cms.ccm.ftp.UltraFTPClient ftpDownload
信息: FTP涓嬭浇浠庢湇鍔″櫒涓婄殑/ddd涓嬭浇鍒版湰鍦?c:/qqq缁撴潫锛?
Process finished with exit code 0
本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/120222,如需转载请自行联系原作者