import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
* ftp操作工具类
* @author dell
*
*/
public class FtpClientUtil {
/**
* 打开ftp服务器连接
*
* @param ftpClient
* @param server
* ftp的url
* @param port
* 端口
* @param userName
* 登陆用户名
* @param password
* 登陆密码
* @return
*/
public static boolean open(FTPClient ftpClient, String server, int port,
String userName, String password) {
if (ftpClient == null) {
ftpClient = new FTPClient();
}
// 已经打开连接则直接返回
if (ftpClient.isConnected())
return true;
try {
// ftpClient.setControlEncoding("GBK");
// 连接服务器
ftpClient.connect(server, port);
ftpClient.login(userName, password);
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
ftpClient.disconnect();
return false;
}
ftpClient.setControlEncoding("GBK");
return true;
} catch (Exception e) {
e.printStackTrace();
ftpClient = null;
return false;
}
}
/**
* 打开ftp上某个目录
*
* @param ftpClient
* @param dir
* @return
*/
public static boolean cd(FTPClient ftpClient, String dir) {
boolean f = false;
try {
ftpClient.changeWorkingDirectory(dir);
} catch (IOException e) {
return f;
}
return true;
}
/**
* 取得指定路径下的满足要求的文件名列表
*
* @param ftpClient
* @param server
* @param port
* @param userName
* @param password
* @param dir
* ftp路径
* @param begin
* 文件名过滤,以什么字符串开始,不需要此过滤传入null
* @param end
* 文件名过滤,以什么字符串结束,不需要此过滤传入null
* @return 出错返回null,正常为文件名list
*/
public static List<String> files(FTPClient ftpClient, String dir,
String begin, String end) {
try {
// 取得文件名流
cd(ftpClient, dir);
FTPFile[] names = ftpClient.listFiles();
List<String> files = new ArrayList<String>();
// 读取文件名
for (FTPFile name : names) {
// 文件名过滤
if (begin != null && !name.getName().startsWith(begin)) {
continue;
}
if (end != null && !name.getName().endsWith(end)) {
continue;
}
files.add(name.getName());
}
return files;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 上传文件到FTP服务器
*
* @param is
* 本地文件流
* @param ftpFileName
* 上传后的文件名
* @param ftpDirectory
* FTP目录如:/path1/pathb2/,如果目录不存在回自动创建目录
* @throws Exception
*/
public static boolean upload(FTPClient ftpClient, InputStream is,
String ftpFileName, String ftpDirectory) throws Exception {
try {
ftpClient.makeDirectory(ftpDirectory);
cd(ftpClient, ftpDirectory);
ftpClient.storeFile(ftpFileName, is);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 从FTP服务器上下载文件并返回文件流
*
* @param ftpDirAndFileName
* @param ftpClient
* @return
* @throws Exception
*/
public static InputStream download2File(FTPClient ftpClient,
String ftpDirAndFileName) throws Exception {
try {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
return ftpClient.retrieveFileStream(charEncode(ftpDirAndFileName));
} catch (Exception e) {
return null;
}
}
/**
* 从FTP服务器上下载文件并返回文件流
*
* @param ftpDirAndFileName
* @param localDirAndFileName
* @param ftpClient
* @return
* @throws Exception
*/
public static OutputStream download2File(FTPClient ftpClient,
String ftpDirAndFileName, File f)
throws Exception {
try {
OutputStream os = new FileOutputStream(f);
ftpClient.setBufferSize(1024);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.retrieveFile(charEncode(ftpDirAndFileName), os);
return os;
} catch (Exception e) {
return null;
}
}
/**
* 删除FTP上的文件
*
* @param ftpDirAndFileName
*/
public static boolean deleteFile(FTPClient ftpClient,
String ftpDirAndFileName) throws Exception {
try {
ftpClient.deleteFile(charEncode(ftpDirAndFileName));
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
/**
* 关闭链接
*/
public static void close(FTPClient ftpClient) {
try {
if (ftpClient != null && ftpClient.isConnected())
ftpClient.logout();
ftpClient.disconnect();
} catch (Exception e) {
}
}
/**
* 转码
* @param str
* @return
* @throws Exception
*/
public static String charEncode(String str) throws Exception {
return new String((str).getBytes("GBK"), "iso-8859-1");
}
}
java使用commons-net-2.2访问ftp
最新推荐文章于 2022-12-30 21:12:12 发布