1.maven依赖
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
2.代码
package com.demo.util;
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;
import java.util.ArrayList;
import java.util.List;
public class FtpUtil {
private static void init(FTPClient ftpClient, String host, String port) {
try {
ftpClient.setControlEncoding("utf-8");
ftpClient.connect(ParamUtil.host, Integer.parseInt(ParamUtil.port));
ftpClient.login(ParamUtil.username, ParamUtil.password);
int replyCode = ftpClient.getReplyCode();
System.out.println("replyCode:" + replyCode);
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("connect failed ... ftp服务器:" + ParamUtil.host + ":" + ParamUtil.port);
} else {
System.out.println("connect successful ... ftp服务器:" + ParamUtil.host + ":" + ParamUtil.port);
}
} catch (IOException e) {
System.out.println("FTP服务器初始化异常");
e.printStackTrace();
}
}
public static boolean uploadFile(String host, String port, File file, String remotePath, String remoteName) {
long startTime = System.currentTimeMillis();
boolean result = false;
FTPClient ftpClient = null;
InputStream inputStream = null;
try {
ftpClient = new FTPClient();
init(ftpClient, host, port);
createFtpDir(ftpClient, remotePath);
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setBufferSize(1024 * 1024);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
inputStream = new FileInputStream(file);
result = ftpClient.storeFile(remoteName, inputStream);
System.out.println("XML文件:" + remoteName + (result ? "上传成功" : "上传失败"));
System.out.println("上传耗时:" + (System.currentTimeMillis() - startTime) / 1000.0);
} catch (IOException e) {
System.out.println("上传功能异常");
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭流异常");
}
}
close(ftpClient);
}
return result;
}
public static List<String> downloadFiles(String host, String port, String remotePath, List<String> nameList, String localDir) {
List<String> localList = new ArrayList<>();
long startTime = System.currentTimeMillis();
FTPClient ftpClient = null;
try {
ftpClient = new FTPClient();
init(ftpClient, host, port);
boolean changeFlag = ftpClient.changeWorkingDirectory(remotePath);
if (!changeFlag) {
System.out.println("没能在FTP服务器找到该目录:" + remotePath);
}
FTPFile[] ftpFiles = ftpClient.listFiles();
for (FTPFile ftpFile : ftpFiles) {
String ftpFileName = ftpFile.getName();
if (nameList.contains(ftpFileName)) {
File localDirFile = new File(localDir);
if (!localDirFile.exists()) {
localDirFile.mkdirs();
}
String localPath = localDir + "/" + ftpFileName;
File localFile = new File(localPath);
OutputStream outputStream = new FileOutputStream(localFile);
boolean downloadFlag = ftpClient.retrieveFile(ftpFileName, outputStream);
if (downloadFlag) {
System.out.println("文件:" + ftpFileName + "下载成功");
localList.add(localPath);
if ("true".equals(ParamUtil.isDelete)) {
ftpClient.deleteFile(ftpFileName);
}
}
outputStream.close();
}
}
System.out.println("下载耗时:" + (System.currentTimeMillis() - startTime) / 1000.0);
return localList;
} catch (IOException e) {
System.out.println("下载功能异常");
e.printStackTrace();
} finally {
close(ftpClient);
}
return localList;
}
private static void close(FTPClient ftpClient) {
if (ftpClient != null) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭FTP Server异常");
}
}
}
private static boolean createFtpDir(FTPClient ftpClient, String path) throws IOException {
if (!ftpClient.isConnected() || !ftpClient.isAvailable()) {
System.out.println("FTP连接已经关闭或者连接无效");
return false;
}
String directory = path + "/";
if (!directory.equalsIgnoreCase("/") && !ftpClient.changeWorkingDirectory(directory)) {
int start = 0;
if (directory.startsWith("/")) {
start = 1;
}
int end = directory.indexOf("/", start);
String pathOne = "";
String pathTwo = "";
do {
String subDirectory = new String(path.substring(start, end).getBytes("GBK"), StandardCharsets.ISO_8859_1);
pathOne = pathOne + "/" + subDirectory;
if (!existFile(ftpClient, pathOne)) {
if (ftpClient.makeDirectory(subDirectory)) {
ftpClient.changeWorkingDirectory(subDirectory);
} else {
ftpClient.changeWorkingDirectory(subDirectory);
}
} else {
ftpClient.makeDirectory(subDirectory);
}
pathTwo = pathTwo + "/" + subDirectory;
start = end + 1;
end = directory.indexOf("/", start);
} while (end > start);
}
return true;
}
private static boolean existFile(FTPClient ftpClient, String path) {
boolean flag = false;
if (!ftpClient.isConnected() || !ftpClient.isAvailable()) {
System.out.println("FTP连接已经关闭或者连接无效");
return flag;
}
try {
FTPFile[] fileArr = ftpClient.listFiles(path);
if (fileArr.length > 0) {
flag = true;
}
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
}