java FTP各种操作

package com.ailk.qw.util;

import it.sauronsoftware.ftp4j.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;



public class FtpUtil {
final static Logger log = LoggerFactory.getLogger(FtpUtil.class);

public static FTPClient getFtp(String ip, int port, String username, String password, String charSet) throws FTPException, IOException, FTPIllegalReplyException {
FTPClient client = new FTPClient();
client.connect(ip, port);
client.login(username, password);
client.setCharset(charSet);
client.setType(FTPClient.TYPE_BINARY);//二进制
return client;
}


/**
* 远程移动文件
*
* @param ip
* @param port
* @param username
* @param password
* @param fromPath
* @param fromFileName
* @param toPath
* @param toFileName
* @return
*/
public static boolean remoteMove(String ip, int port, String username, String password, String fromPath, String fromFileName, String toPath, String toFileName) {
boolean result = false;
FTPClient client = null;
log.info("开始将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName);
try {
client = getFtp(ip, port, username, password, "UTF-8");
//client.changeDirectory(fromPath);
client.rename(fromPath + "/" + fromFileName, toPath + "/" + toFileName);
client.disconnect(true);
if (client.isConnected()) {
client.disconnect(false);
}
result = true;
} catch (FTPException e) {
log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
} catch (IOException e) {
log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
} finally {
if (client.isConnected()) {
try {
client.disconnect(false);
} catch (IOException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPException e) {
log.error("关闭ftp连接时出现异常", e);
}
}
log.info("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "结果为:" + result);
return result;
}

}

/**
* 远程移动文件
*
* @param ip
* @param port
* @param username
* @param password
* @param fromPath
* @param fromFileName
* @param toPath
* @param toFileName
* @param charSet
* @return
*/
public static boolean remoteMove(String ip, int port, String username, String password, String fromPath, String fromFileName, String toPath, String toFileName, String charSet) {
boolean result = false;
FTPClient client = null;
log.info("开始将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName);
try {
client = getFtp(ip, port, username, password, charSet);
//client.changeDirectory(fromPath);
client.rename(fromPath + "/" + fromFileName, toPath + "/" + toFileName);
client.disconnect(true);
if (client.isConnected()) {
client.disconnect(false);
}
result = true;
} catch (FTPException e) {
log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
} catch (IOException e) {
log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
} finally {
if (client.isConnected()) {
try {
client.disconnect(false);
} catch (IOException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPException e) {
log.error("关闭ftp连接时出现异常", e);
}
}
log.info("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "结果为:" + result);
return result;
}

}

/**
* 下载文件
*
* @param ip
* @param port
* @param username
* @param password
* @param remotePath
* @param remoteFileName
* @param localPath
* @param localFileName
* @return
*/
public static boolean download(String ip, int port, String username, String password, String remotePath, String remoteFileName, String localPath, String localFileName) {
boolean result = false;
FTPClient client = null;
log.info("开始从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName);
try {
client = getFtp(ip, port, username, password, "UTF-8");
client.changeDirectory(remotePath);
client.download(remoteFileName, new File(localPath + File.separatorChar + localFileName));
client.disconnect(true);
if (client.isConnected()) {
client.disconnect(false);
}
result = true;
} catch (FTPException e) {
log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
} catch (IOException e) {
log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
} catch (FTPIllegalReplyException e) {
log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
} catch (FTPAbortedException e) {
log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
} catch (FTPDataTransferException e) {
log.info("从" + ip + "服务器传目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
} finally {
if (client.isConnected()) {
try {
client.disconnect(false);
} catch (IOException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPException e) {
log.error("关闭ftp连接时出现异常", e);
}
}
log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "结果为:" + result);
return result;
}
}

/**
* 上传文件
*
* @param ip
* @param port
* @param username
* @param password
* @param remotePath
* @param file
* @return
*/
public static boolean upload(String ip, int port, String username, String password, String remotePath, File file, String oldFileName, String fileName) {
FTPClient client = null;
boolean result = false;
try {
log.info("开始向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName);
client = getFtp(ip, port, username, password, "UTF-8");
client.changeDirectory(remotePath);
client.changeDirectoryUp();
client.changeDirectory("temp");
String fromPath = client.currentDirectory();
String fromFileName = oldFileName;
client.upload(file);
client.disconnect(true);
if (client.isConnected()) {
client.disconnect(false);
}
result = remoteMove(ip, port, username, password, fromPath, fromFileName, remotePath, fileName);
} catch (FTPException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (IOException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (FTPIllegalReplyException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (FTPAbortedException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (FTPDataTransferException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} finally {
if (client.isConnected()) {
try {
client.disconnect(false);
} catch (IOException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPException e) {
log.error("关闭ftp连接时出现异常", e);
}
}
log.info("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "结果为:" + result);
return result;
}
}

/**
* 上传文件
*
* @param ip
* @param port
* @param username
* @param password
* @param remotePath
* @param file
* @param charSet
* @return
*/
public static boolean upload(String ip, int port, String username, String password, String remotePath, File file, String oldFileName, String fileName, String charSet) {
FTPClient client = null;
boolean result = false;
try {
log.info("开始向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName);
client = getFtp(ip, port, username, password, charSet);
client.changeDirectory(remotePath);
client.changeDirectoryUp();
client.changeDirectory("temp");
String fromPath = client.currentDirectory();
String fromFileName = oldFileName;
client.upload(file);
client.disconnect(true);
if (client.isConnected()) {
client.disconnect(false);
}
result = remoteMove(ip, port, username, password, fromPath, fromFileName, remotePath, fileName, charSet);
} catch (FTPException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (IOException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (FTPIllegalReplyException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (FTPAbortedException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} catch (FTPDataTransferException e) {
log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
} finally {
if (client.isConnected()) {
try {
client.disconnect(false);
} catch (IOException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPIllegalReplyException e) {
log.error("关闭ftp连接时出现异常", e);
} catch (FTPException e) {
log.error("关闭ftp连接时出现异常", e);
}
}
log.info("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "结果为:" + result);
return result;
}
}


}
原文地址:https://www.cnblogs.com/xiaoyu1994/p/9110803.html