首页 > 代码库 > FTP 文件操场 FTPClient
FTP 文件操场 FTPClient
工作中需要实现对远程FTP文件的各种操作,简单写了个静态FTP操作工具类,以下源码供大家参考:
public class FtpUtil {
private static FTPClient ftpClient = new FTPClient();
private static String encoding = System.getProperty("file.encoding");
private static String defaultPath;
/**
* FTP初始化连接
*/
public static boolean connect(String host,Integer port,String userName,String password) throws IOException{
return connect(host,port,userName,password,null);
}
/**
* FTP初始化连接
*/
public static boolean connect(String host,Integer port,String userName,String password,String remotePath) throws IOException{
boolean result=false;
try {
ftpClient.connect(host,port);
// 登录
ftpClient.login(userName, password);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 检验是否连接成功
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {//连接失败
ftpClient.disconnect();
result =false;
}else{//如果连接成功
result = true;
if(remotePath != null){
defaultPath = remotePath;
result =ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));
if(result == false){
System.err.println("ftp changeWorkingDirectory to ".concat(remotePath).concat(" error"));
}
}
System.out.println("ftp connect successfully.");
}
} catch (SocketException e) {
System.err.println("ftp connect failed: " + e.getMessage());
throw e;
} catch (IOException e) {
System.err.println("ftp connect failed: " + e.getMessage());
throw e;
}
return result;
}
/**
* FTP是否连接
*/
public static boolean isConnected(){
return ftpClient.isConnected();
}
/**
* 上传文件
*/
public static boolean uploadFile(String filename, InputStream input) throws IOException {
return uploadFile(null,filename,input);
}
/**
* 上传文件
*/
public static boolean uploadFile(String remotePath, String filename, InputStream input) throws IOException {
boolean result = false;
if (!ftpClient.isConnected()) {
System.err.println("ftp uploadfile error : no ftpserver is connected");
return false;
}
try {
// 转移工作目录至指定目录下
if(remotePath != null){
ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
result = ftpClient.storeFile(new String(filename.getBytes(encoding),"iso-8859-1"), input);
} catch (IOException e) {
System.err.println("ftp uploadfile error : "+e.getMessage());
throw e;
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException e) {
System.err.println("ftp uploadfile close error : "+e.getMessage());
throw e;
}
}
System.out.println("ftp uploadfile successfully.");
return result;
}
/**
* 下载FTP文件
*/
public static boolean downFile(String fileName, String localPath) throws IOException {
return downFile(null,fileName,localPath);
}
/**
* 下载FTP文件
*/
public static boolean downFile( String remotePath, String fileName, String localPath) throws IOException {
boolean result = false;
if (!ftpClient.isConnected()) {
System.err.println("ftp download file error : no ftpserver is connected");
return false;
}
try {
// 转移工作目录至指定目录下
if(remotePath != null){
ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));
}
System.out.println("ftp start download file :" + fileName);
// 获取文件列表
FTPFile[] fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if(fileName != null && !fileName.equals(ff.getName())){
continue;
}
File localFile = new File(localPath + "/" + ff.getName());
OutputStream os = new FileOutputStream(localFile);
ftpClient.retrieveFile(ff.getName(), os);
os.close();
System.out.println("ftp download file finished :"+ff.getName());
result = true;
}
if(result == false){
System.err.println("ftp download error cannot find file : " + fileName);
}
} catch (IOException e) {
System.err.println("ftp download error : "+e.getMessage());
throw e;
}
return result;
}
/**
* 关闭当前连接
*/
public static void close(){
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
System.err.println("ftp ftpserver close error : "+e.getMessage());
}
}
/**
* 返回连接默认路径
*/
public static void cdRoot() throws Exception{
try {
if (!ftpClient.isConnected()) {
System.err.println("ftp cd root error : no ftpserver is connected");
}
ftpClient.changeWorkingDirectory(new String("/".getBytes(encoding),"iso-8859-1"));
if(defaultPath != null){
ftpClient.changeWorkingDirectory(new String(defaultPath.getBytes(encoding),"iso-8859-1"));
}
} catch (Exception e) {
System.err.println("ftp change directory to defaultpath error : "+e.getMessage());
throw e;
}
}
/**
* 工作路径切换
*/
public static void cd(String path) throws Exception{
try {
if (!ftpClient.isConnected()) {
System.err.println(" ftp cd " + path + " : no ftpserver is connected");
}
ftpClient.changeWorkingDirectory(new String(path.getBytes(encoding),"iso-8859-1"));
} catch (Exception e) {
System.err.println("ftp cd ‘" + path + "‘ error : "+e.getMessage());
throw e;
}
}
/**
* 创建工作路径
*/
public static void mkdir(String path) throws IOException{
try {
if (!ftpClient.isConnected()) {
System.err.println("ftp mkdir error : no ftpserver is connected");
}
ftpClient.makeDirectory(path);
} catch (IOException e) {
System.err.println("ftp mkdir ‘" + path + "‘ error : "+e.getMessage());
throw e;
}
}
/**
* 查找文件是否存在
*/
public static FTPFile findFile(String filename) throws IOException{
return findFile(null,filename);
}
/**
* 查找文件是否存在
*/
public static FTPFile findFile(String remotePath, String filename) throws IOException{
try {
if (!ftpClient.isConnected()) {
System.err.println("ftp findfile error : no ftpserver is connected");
return null;
}
FTPFile[] fs = ftpClient.mlistDir(remotePath);
for (FTPFile ff : fs) {
if(filename != null && !filename.equals(ff.getName())){
continue;
}
return ff;
}
} catch (IOException e) {
System.err.println("ftp find file ‘" + filename + "‘ error : "+e.getMessage());
throw e;
}
return null;
}
}
FTP 文件操场 FTPClient