import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPFileFilter;
import org.apache.commons.net.ftp.FTPReply;
import app.cn.qtt.cmbase.system.CCrppLog4j;
public class FTPUtil {
private static CCrppLog4j log = new CCrppLog4j(FTPUtil.class.getName());
/**
* 关闭FTP连接
*
* @param client
*/
public static void disconnect(FTPClient client) {
final String xFunctionName = "disconnect";
if (client != null && client.isConnected()) {
try {
log.debug("开始关闭FTP连接……");
client.disconnect();
log.debug("FTP连接关闭成功!");
} catch (IOException e) {
log.exception(xFunctionName, e);
}
}
}
/**
* 下载FTP文件_文件名与源一致
*
* @param url
* @param port
* @param username
* @param password
* @param remotePath
* @param fileName
* @param localPath
* @return
*/
public static boolean DownLoadFile(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath) {
final String xFunctionName = "DownLoadFile";
log.begin(xFunctionName);
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
log.exception(xFunctionName , e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
log.end(xFunctionName);
}
return success;
}
/**
* 下载FTP文件_文件名按传入重命名
*
* @param url
* @param port
* @param username
* @param password
* @param remotePath
* @param fileName
* @param localPath
* @param localName
* @return
*/
public static boolean DownLoadFile(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath, String localName) {
final String xFunctionName = "DownLoadFile";
log.begin(xFunctionName);
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
ftp.enterLocalPassiveMode();
// InetAddress addr = InetAddress.getByName(url);
// boolean mode = ftp.enterRemoteActiveMode(addr, 21);
log.info("url:" + url + ",remotePath:" + remotePath + ",fileName:+"
+ fileName + "localPath:" + localPath);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + localName);
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
log.exception(xFunctionName, e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
log.end(xFunctionName);
}
return success;
}
public static String DownLoadFileForFilter(String url, int port,
String username, String password, String remotePath,
String fileName, String localPath) {
final String xFunctionName = "DownLoadFileForFilter";
log.begin(xFunctionName);
String downloadFileName = null;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
ftp.enterRemotePassiveMode();
ftp.enterLocalPassiveMode();
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return null;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().startsWith(fileName)
&& ff.getName().endsWith(".AVL")) {// TODO
// 此处后缀写死了
File localFile = new File(localPath + "/" + ff.getName());
downloadFileName = ff.getName();
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
} catch (Exception e) {
log.exception(xFunctionName, e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
log.end(xFunctionName);
}
return downloadFileName;
}
public static String DownLoadFileWithFilter(String url, int port,
String username, String password, String remotePath,
String fileName, String localPath, FTPFileFilter filter) {
String downloadFileName = null;
System.out.println("DownLoadFileForFilter执行开始");
FTPClient ftp = new FTPClient();
System.out.println("FTPClient ftp = new FTPClient()执行完毕");
try {
int reply;
System.out.println("ftp.connect(url, port);");
ftp.connect(url, port);
ftp.enterRemotePassiveMode();
ftp.enterLocalPassiveMode();
System.out.println("ftp.connect(url, port);执行结束");
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
System.out.println("ftp.login(username, password);登录开始");
ftp.login(username, password);// 登录
System.out.println("ftp.login(username, password);登录结束");
reply = ftp.getReplyCode();
System.out.println("reply=" + reply);
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return null;
}
System.out.println("FTPFile[] fs = ftp.listFiles();开始执行");
FTPFile[] fs = ftp.listFiles(remotePath, filter);
System.out.println("FTPFile[] fs = ftp.listFiles();执行结束");
for (FTPFile ff : fs) {
File localFile = new File(localPath + "/" + ff.getName());
downloadFileName = ff.getName();
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
ftp.logout();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return downloadFileName;
}
public static boolean removeFile(String url, int port, String username,
String password, String remotePath, String fileName) {
boolean flag = false;
FTPClient ftpClient = new FTPClient();
if (ftpClient != null) {
try {
int reply;
ftpClient.connect(url, port);
ftpClient.login(username, password);// 登录
reply = ftpClient.getReplyCode();
log.info("reply=" + reply);
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return flag;
}
ftpClient.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
// 删除文件
flag = ftpClient.deleteFile(fileName);
ftpClient.logout();
flag = true;
} catch (IOException e) {
log.error(e.getMessage());
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
log.error(ioe.getMessage());
}
}
}
}
return flag;
}
public static boolean renameFile(String url, int port, String username,
String password, String remotePath, String fileName,
String newFileName) {
boolean flag = false;
FTPClient ftpClient = new FTPClient();
if (ftpClient != null) {
try {
int reply;
ftpClient.connect(url, port);
ftpClient.login(username, password);// 登录
reply = ftpClient.getReplyCode();
log.info("reply=" + reply);
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return flag;
}
ftpClient.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
// 删除文件
flag = ftpClient.rename(fileName, newFileName);
ftpClient.logout();
flag = true;
} catch (IOException e) {
log.error(e.getMessage());
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
log.error(ioe.getMessage());
}
}
}
}
return flag;
}
/**
* 下载FTP文件
*
* @param hostName
* @param hostPort
* @param usr
* @param pwd
* @param remotefile
* @param localfile
* @param encode
* @return
* @throws IOException
*/
public static boolean DownLoadFileFromFtp(String hostName, int hostPort,
String usr, String pwd, String remotefile, String localfile,
String encode) throws Exception {
boolean bl = false;
FTPClient ftpc = null;
FileOutputStream out = null;
try {
ftpc = new FTPClient();
ftpc.setControlEncoding(encode);
ftpc.connect(hostName, hostPort);
ftpc.login(usr, pwd);
out = new FileOutputStream(localfile);
ftpc.retrieveFile(remotefile, out);
bl = true;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
try {
if (out != null) {
out.close();
}
if (ftpc != null) {
ftpc.logout();
ftpc.disconnect();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return bl;
}
/**
* 从FTP服务器上下载文件
*
* @param hostname
* FTP服务器主机名
* @param port
* FTP服务端口
* @param remoteDirectory
* 远程目录
* @param username
* 用户名
* @param password
* 密码
* @param encoding
* 使用编码
* @param localDirectory
* 本地目录
* @param deepDownload
* 是否遍历下载
* @param needDelete
* 下载完成后是否删除服务器上的文件
* @return
*/
public static List<FTPOperationLog> downloadFiles(final String hostname,
final int port, final String remoteDirectory,
final String username, String password, final String encoding,
final String localDirectory, final boolean deepDownload,
final boolean needDelete) {
List<FTPOperationLog> list = Collections.emptyList();
try {
list = new FTPDownloadTools(hostname, port, remoteDirectory,
username, password, encoding, localDirectory, deepDownload,
needDelete).downloadFiles();
} catch (IOException e) {
log.exception("downloadFiles", e);
}
return list;
}
/**
* 获取FTP远程目录名
*
* @param path
* @return
*/
public static String getDirectoryFrom(String path) {
if (StringUtils.indexOf(path, "/") == 0) {
return getDirectoryFrom(path.substring(1, path.length()));
}
return path;
}
/**
* @param hostname
* @param port
* @param remoteDirectory
* @param username
* @param password
* @param encoding
* @param localDirectory
* @param traversal
* @param needDelete
* @return
*/
public static List<FTPOperationLog> uploadFiles(final String hostname,
final int port, final String remoteDirectory,
final String username, String password, final String encoding,
final String localDirectory, final boolean traversal,
final boolean needDelete) {
List<FTPOperationLog> list = Collections.emptyList();
try {
list = new FTPUploadTools(hostname, port, remoteDirectory,
username, password, encoding, localDirectory, traversal,
needDelete).uploadFiles();
} catch (IOException e) {
log.exception("uploadFiles", e);
}
return list;
}
/**
* 通过APACHE包上传到FTP
*
* @param hostName
* @param hostPort
* @param usr
* @param pwd
* @param srcfile
* @param destfile
* @param encode
* @return
*/
public static boolean uploadToFtpByApacheFtp(String hostName, int hostPort,
String usr, String pwd, String srcfile, String destfile,
String encode) {
boolean bl = false;
FTPClient ftpc = null;
FileInputStream in = null;
try {
ftpc = new FTPClient();
ftpc.setControlEncoding(encode);
ftpc.connect(hostName, hostPort);
ftpc.login(usr, pwd);
in = new FileInputStream(srcfile);
ftpc.storeFile(destfile, in);
bl = true;
} catch (Exception e) {
e.printStackTrace();
bl = false;
} finally {
try {
if (in != null) {
in.close();
}
if (ftpc != null) {
ftpc.logout();
ftpc.disconnect();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return bl;
}
/**
* 通过APACHE包上传到FTP
*
* @param hostName
* @param hostPort
* @param usr
* @param pwd
* @param srcfile
* @param destfile
* @param encode
* @return
*/
public static boolean mkDirectByApacheFtp(String hostName, int hostPort,
String usr, String pwd, String pathname, String encode) {
boolean bl = false;
FTPClient ftpc = null;
try {
ftpc = new FTPClient();
ftpc.setControlEncoding(encode);
ftpc.connect(hostName, hostPort);
ftpc.login(usr, pwd);
bl = ftpc.makeDirectory(pathname);
} catch (Exception e) {
e.printStackTrace();
bl = false;
} finally {
try {
if (ftpc != null) {
ftpc.logout();
ftpc.disconnect();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return bl;
}
// /**
// * 通过sun自带方法上传到FTP
// *
// * @param hostName
// * @param hostPort
// * @param usr
// * @param pwd
// * @param srcfile
// * @param destfile
// * @param isAscii
// * @return
// * @throws IOException
// */
// public static boolean uploadToFtpServer(String hostName, int hostPort,
// String usr, String pwd, String srcfile, String destfile,
// boolean isAscii) throws IOException {
// boolean bl = false;
// FtpClient ftpc = null;
// FileInputStream in = null;
// FileOutputStream out = null;
// try {
// //ftpc = new FtpClient(hostName, hostPort);
// ftpc = FtpClient.create(new InetSocketAddress(hostName, hostPort));
// if (isAscii) {
// ftpc.setAsciiType();
// } else {
// ftpc.setBinaryType();
// }
// ftpc.login(usr, null, pwd);
// in = new FileInputStream(srcfile);
// out = new FileOutputStream(destfile);
// byte[] bt = new byte[1024];
// while (in.read(bt) != -1) {
// out.write(bt, 0, bt.length);
// }
// bl = true;
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// if (out != null) {
// out.close();
// }
// if (in != null) {
// in.close();
// }
// if (ftpc != null) {
// ftpc.close();
// }
// }
// return bl;
// }
/**
* 下载FTP文件_文件名与源一致
*
* @param url
* @param port
* @param username
* @param password
* @param remotePath
* @param fileName
* @param localPath
* @param bl_passive
* @return
*/
public static boolean DownLoadFile(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath, boolean bl_passive) {
final String xFunctionName = "DownLoadFile";
log.begin(xFunctionName);
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
ftp.enterLocalPassiveMode();
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
// 由于北京FTP服务器日期设置成中文无法正常LISTFILE,临时用这种方式_Begin
File localFile = null;
OutputStream is = null;
localFile = new File(localPath + "/" + fileName);
is = new FileOutputStream(localFile);
ftp.retrieveFile(fileName, is);
is.close();
// 由于北京FTP服务器日期设置成中文无法正常LISTFILE,临时用这种方式_End
ftp.logout();
success = true;
} catch (IOException e) {
log.exception(xFunctionName , e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
log.end(xFunctionName);
}
return success;
}
public static boolean DownLoadFileV2(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath, boolean bl_passive) {
final String xFunctionName = "DownLoadFile";
log.begin(xFunctionName);
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
// ftp.enterLocalPassiveMode();
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
// 由于北京FTP服务器日期设置成中文无法正常LISTFILE,临时用这种方式_Begin
File localFile = null;
OutputStream is = null;
localFile = new File(localPath + "/" + fileName);
is = new FileOutputStream(localFile);
ftp.retrieveFile(fileName, is);
is.close();
// 由于北京FTP服务器日期设置成中文无法正常LISTFILE,临时用这种方式_End
ftp.logout();
success = true;
} catch (IOException e) {
log.exception(xFunctionName , e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
log.end(xFunctionName);
}
return success;
}
public static void main(String[] args){
FTPFile[] fs=FTPUtil.listFiles("127.0.0.1", 11600, "username", "password", "path");
System.out.println(fs.length);
}
/**
* @title 获取指定远程目录中所有文件及文件夹
* @param url
* @param port
* @param username
* @param password
* @param remotePath
* @param fileName
* @param localPath
*/
public static FTPFile[] listFiles(String url, int port, String userName,
String pwd, String remotePath) {
final String xFunctionName = "listFiles";
log.begin(xFunctionName);
FTPClient ftp = new FTPClient();
FTPFile[] fs = null;
try {
int reply;
ftp.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(userName, pwd);// 登录
//ftp.enterLocalPassiveMode();
//ftp.enterRemotePassiveMode();
reply = ftp.getReplyCode();
log.info("reply" + reply);
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
log.info("disconnect!!!!!!!!!!!!");
return null;
}
System.out.println("切换到远程目录"+remotePath+ftp.changeWorkingDirectory(remotePath));// 转移到FTP服务器目录
fs = ftp.listFiles();
log.info(fs.length > 1 ? fs[0].getName() : "没有文件");
ftp.logout();
} catch (IOException e) {
fs = null;
log.exception(xFunctionName, e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
log.exception(xFunctionName, ioe);
}
}
log.end(xFunctionName);
}
return fs;
}
public static FTPFile[] listFilesWithFilter(String url, int port,
String userName, String pwd, String remotePath, FTPFileFilter filter) {
final String xFunctionName = "listFilesWithFilter";
log.begin(xFunctionName);
FTPClient ftp = new FTPClient();
FTPFile[] fs = null;
try {
int reply;
ftp.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(userName, pwd);// 登录
ftp.enterLocalPassiveMode();
// ftp.enterRemotePassiveMode();
reply = ftp.getReplyCode();
log.info("reply" + reply);
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
log.info("disconnect!!!!!!!!!!!!");
return null;
}
//ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
// fs = ftp.listFiles();
fs = ftp.listFiles(remotePath, filter);
log.info(fs.length > 1 ? fs[0].getName() : "没有文件");
ftp.logout();
} catch (IOException e) {
fs = null;
log.exception(xFunctionName, e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
log.exception(xFunctionName, ioe);
}
}
log.end(xFunctionName);
}
return fs;
}
/**
* @title 修改指定文件名字
* @param url
* @param port
* @param userName
* @param pwd
* @param remotePath
* @param newFileName新的文件名字
* @return
*/
public static boolean modifyFileName(String url, int port, String userName,
String pwd, String remotePath, String fileName, String newFileName) {
final String xFunctionName = "modifyFileName";
log.begin(xFunctionName);
FTPClient ftp = new FTPClient();
boolean success = false;
try {
int reply;
ftp.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(userName, pwd);// 登录
// ftp.enterLocalPassiveMode();
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
log.info("重命名:" + ftp.rename(fileName, newFileName));
ftp.logout();
success = true;
} catch (IOException e) {
success = false;
log.exception(xFunctionName, e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
log.exception(xFunctionName, ioe);
}
}
log.end(xFunctionName);
}
return success;
}
/**
*
* @param file
* 上传的文件或文件夹
* @throws Exception
*/
public static boolean upload(String url, int port, String userName,
String pwd, String remotePath, File file) {
String xFunctionName ="upload";
log.begin(xFunctionName);
FTPClient ftp = new FTPClient();
boolean success = false;
try {
ftp.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(userName, pwd);// 登录
ftp.enterLocalPassiveMode();//被动模式
ftp.changeWorkingDirectory(remotePath);
if (file.isDirectory()) {
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath() + "\\" + files[i]);
if (file1.isDirectory()) {
upload(url, port, userName, pwd, remotePath, file);
ftp.changeToParentDirectory();
} else {
File file2 = new File(file.getPath() + "\\" + files[i]);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
} else {
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
ftp.logout();
success = true;
} catch (IOException e) {
success = false;
log.exception(xFunctionName, e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
log.exception(xFunctionName, ioe);
}
}
log.end(xFunctionName);
}
return success;
}
/**
*
* @param file
* 上传的文件或文件夹
* @throws Exception
*/
public static boolean uploadByPassiveMode(String url, int port, String userName,
String pwd, String remotePath, File file) {
String xFunctionName ="upload";
log.begin(xFunctionName);
FTPClient ftp = new FTPClient();
boolean success = false;
try {
ftp.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(userName, pwd);// 登录
ftp.changeWorkingDirectory(remotePath);
if (file.isDirectory()) {
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath() + "\\" + files[i]);
if (file1.isDirectory()) {
upload(url, port, userName, pwd, remotePath, file);
ftp.changeToParentDirectory();
} else {
File file2 = new File(file.getPath() + "\\" + files[i]);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
} else {
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
log.info("ftp.storeFile(file2.getName(), input)方法调用开始。");
ftp.storeFile(file2.getName(), input);
log.info("ftp.storeFile(file2.getName(), input)方法调用结束。");
input.close();
}
ftp.logout();
success = true;
} catch (Exception e) {
success = false;
log.exception(xFunctionName, e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
log.exception(xFunctionName, ioe);
}
}
log.end(xFunctionName);
}
return success;
}
/**
* 下载FTP文件_文件名按传入重命名
*
* @param url
* @param port
* @param username
* @param password
* @param remotePath
* @param fileName
* @param localPath
* @param localName
* @return
*/
public static boolean downLoadFileByRetrive(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath) {
final String xFunctionName = "DownLoadFile";
log.begin(xFunctionName);
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
ftp.enterLocalPassiveMode();
// InetAddress addr = InetAddress.getByName(url);
// boolean mode = ftp.enterRemoteActiveMode(addr, 21);
log.info("url:" + url + ",remotePath:" + remotePath + ",fileName:+"
+ fileName + "localPath:" + localPath);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
File localFile = new File(localPath + "/" + fileName);
OutputStream is = new FileOutputStream(localFile);
boolean result = ftp.retrieveFile(fileName, is);
is.close();
ftp.logout();
success = result;
} catch (IOException e) {
log.exception(xFunctionName, e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
log.end(xFunctionName);
}
return success;
}
}
FTP工具类
最新推荐文章于 2025-02-05 11:34:27 发布