java ftp 上传下载文件工具类
package antmap.web.oa.common;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class FtpUtils {
private String _hostname;
private Integer _port = 21;
private String _username;
private String _password;
private String _ftpDir;
private FTPClient _ftpClient = null;
public FtpUtils() {
ResourceBundle resourceBundle = ResourceBundle.getBundle("config/ftp");
_hostname = resourceBundle .getString("ftpServer");
_port = Integer.parseInt(resourceBundle .getString("ftpPort"));
_username = resourceBundle .getString("ftpUser");
_password = resourceBundle .getString("ftpPsw");
_ftpDir = resourceBundle .getString("ftpDir");
}
public FtpUtils(String hostname, String username, String password) {
_hostname = hostname;
_username = username;
_password = password;
}
public FtpUtils(String hostname, Integer port, String username, String password) {
_hostname = hostname;
_port = port;
_username = username;
_password = password;
}
private void initFtpClient() {
_ftpClient = new FTPClient();
try {
Function.outMessage("connecting...ftp服务器:" + this._hostname + ":" + this._port);
_ftpClient.connect(_hostname, _port);
_ftpClient.login(_username, _password);
int replyCode = _ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
Function.outMessage("connect failed...ftp服务器:" + this._hostname + ":" + this._port);
}
Function.outMessage("connect successful...ftp服务器:" + this._hostname + ":" + this._port);
if (FTPReply.isPositiveCompletion(_ftpClient.sendCommand("OPTS UTF8", "ON"))) {
_ftpClient.setControlEncoding("utf-8");
} else {
_ftpClient.setControlEncoding("GBK");
}
if (!StringUtils.isBlank(this._ftpDir)) {
_ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
CreateDirecroty(_ftpDir);
Function.outMessage("connect successful:" + this._ftpDir);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean uploadFile(String pathname, String fileName, String originfilename) {
InputStream inputStream = null;
try {
Function.outMessage("开始上传文件");
inputStream = new FileInputStream(new File(originfilename));
initFtpClient();
_ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
CreateDirecroty(pathname);
_ftpClient.storeFile(fileName, inputStream);
inputStream.close();
_ftpClient.logout();
Function.outMessage("上传文件成功");
} catch (Exception e) {
Function.outMessage("上传文件失败");
e.printStackTrace();
} finally {
if (_ftpClient.isConnected()) {
try {
_ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
public boolean uploadFile(String pathname, String fileName, InputStream inputStream) {
boolean flag = false;
try {
Function.outMessage("开始上传文件");
initFtpClient();
_ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
CreateDirecroty(pathname);
_ftpClient.storeFile(fileName, inputStream);
inputStream.close();
_ftpClient.logout();
flag = true;
Function.outMessage("上传文件成功");
} catch (Exception e) {
Function.outMessage("上传文件失败");
e.printStackTrace();
} finally {
if (_ftpClient.isConnected()) {
try {
_ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
private boolean changeWorkingDirectory(String directory) {
boolean flag = true;
try {
flag = _ftpClient.changeWorkingDirectory(directory);
if (flag) {
Function.outMessage("进入文件夹" + directory + " 成功!");
} else {
Function.outMessage("进入文件夹" + directory + " 失败!开始创建文件夹");
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return flag;
}
private void CreateDirecroty(String remote) throws IOException {
String directory = remote + File.separator;
if (!directory.equalsIgnoreCase(File.separator) && !changeWorkingDirectory(directory)) {
int start = 0;
int end;
if (directory.startsWith(File.separator)) {
start = 1;
}
end = directory.indexOf(File.separator, start);
String path = "";
StringBuilder paths;
paths = new StringBuilder();
do {
String subDirectory = remote.substring(start, end);
path = path + File.separator + subDirectory;
if (!existFile(path)) {
if (makeDirectory(subDirectory)) {
changeWorkingDirectory(subDirectory);
} else {
Function.outMessage("创建目录[" + subDirectory + "]失败");
changeWorkingDirectory(subDirectory);
}
} else {
changeWorkingDirectory(subDirectory);
}
paths.append(File.separator).append(subDirectory);
start = end + 1;
end = directory.indexOf(File.separator, start);
} while (end > start);
}
}
private boolean existFile(String path) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = _ftpClient.listFiles(path);
if (ftpFileArr.length > 0) {
flag = true;
}
return flag;
}
private boolean makeDirectory(String dir) {
boolean flag = true;
try {
flag = _ftpClient.makeDirectory(dir);
if (flag) {
Function.outMessage("创建文件夹" + dir + " 成功!");
} else {
Function.outMessage("创建文件夹" + dir + " 失败!");
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public boolean downloadFile(String pathname, String filename, String localpath) {
boolean flag = false;
OutputStream os = null;
try {
Function.outMessage("开始下载文件");
initFtpClient();
_ftpClient.changeWorkingDirectory(pathname);
FTPFile[] ftpFiles = _ftpClient.listFiles();
for (FTPFile file : ftpFiles) {
if (filename.equalsIgnoreCase(file.getName())) {
File localFile = new File(localpath + File.separator + file.getName());
os = new FileOutputStream(localFile);
_ftpClient.retrieveFile(file.getName(), os);
os.close();
}
}
_ftpClient.logout();
flag = true;
Function.outMessage("下载文件成功");
} catch (Exception e) {
Function.outMessage("下载文件失败");
e.printStackTrace();
} finally {
if (_ftpClient.isConnected()) {
try {
_ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != os) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
public boolean downloadFileSingleToOutputStream(String pathname, OutputStream os) throws IOException {
boolean flag ;
try {
initFtpClient();
_ftpClient.enterLocalPassiveMode();
flag = _ftpClient.retrieveFile(pathname, os);
_ftpClient.logout();
} finally {
if (_ftpClient.isConnected()) {
_ftpClient.disconnect();
}
}
return flag;
}
public boolean downloadFileSingle(String pathname, String localpath) {
boolean flag = false;
OutputStream os = null;
try {
Function.outMessage("开始下载文件");
initFtpClient();
File localFile = new File(localpath);
os = new FileOutputStream(localFile);
_ftpClient.enterLocalPassiveMode();
flag = _ftpClient.retrieveFile(pathname, os);
_ftpClient.logout();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (_ftpClient.isConnected()) {
try {
_ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != os) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (flag) {
Function.outMessage("下载文件成功");
} else {
Function.outMessage("下载文件失败");
}
}
return flag;
}
public boolean deleteFile(String pathname, String filename) {
boolean flag = false;
try {
Function.outMessage("开始删除文件");
initFtpClient();
_ftpClient.changeWorkingDirectory(pathname);
_ftpClient.dele(filename);
_ftpClient.logout();
flag = true;
Function.outMessage("删除文件成功");
} catch (Exception e) {
Function.outMessage("删除文件失败");
e.printStackTrace();
} finally {
if (_ftpClient.isConnected()) {
try {
_ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
public boolean deleteFileSingle(String sPath) {
boolean flag = false;
try {
Function.outMessage("开始删除文件");
initFtpClient();
_ftpClient.dele(sPath);
_ftpClient.logout();
flag = true;
Function.outMessage("删除文件成功");
} catch (Exception e) {
Function.outMessage("删除文件失败");
e.printStackTrace();
} finally {
if (_ftpClient.isConnected()) {
try {
_ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
public boolean copyFile(String olePath, String newPath, String fileName) {
boolean flag = false;
try {
initFtpClient();
_ftpClient.changeWorkingDirectory(olePath);
_ftpClient.enterLocalPassiveMode();
FTPFile[] files = _ftpClient.listFiles();
ByteArrayInputStream in ;
org.apache.commons.io.output.ByteArrayOutputStream out ;
for (FTPFile file : files) {
if (file.getName().equals(fileName)) {
out = new ByteArrayOutputStream();
_ftpClient.retrieveFile(new String(file.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1), out);
in = new ByteArrayInputStream(out.toByteArray());
CreateDirecroty(newPath);
_ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
flag = uploadFile(newPath, fileName, in);
out.flush();
out.close();
in.close();
}
}
} catch (Exception e) {
Function.outMessage("复制已存在edb文件到备份文件夹失败!原因:" + e.getMessage());
} finally {
if (_ftpClient.isConnected()) {
try {
_ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
}