1、引入依赖
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
2、编写工具类
@Slf4j
public class FtpClientUtil {
private FTPClient ftpClient;
private String ip;
private Integer port;
private String user;
private String passWord;
public FtpClientUtil(String ip, Integer port, String user, String passWord) {
this.ip = ip;
this.port = port;
this.user = user;
this.passWord = passWord;
this.ftpClient = new FTPClient();
}
public boolean ftpLogin() {
boolean isLogin = false;
FTPClientConfig ftpClientConfig = new FTPClientConfig();
ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
this.ftpClient.setControlEncoding("GBK");
this.ftpClient.configure(ftpClientConfig);
try {
if (this.port > 0) {
this.ftpClient.connect(this.ip, this.port);
} else {
this.ftpClient.connect(this.ip);
}
int reply = this.ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
this.ftpClient.disconnect();
log.error("登录FTP服务失败!");
return isLogin;
}
this.ftpClient.login(this.user, this.passWord);
this.ftpClient.enterLocalPassiveMode();
this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
log.info(this.user + "成功登陆FTP服务器");
isLogin = true;
} catch (Exception e) {
e.printStackTrace();
log.error(this.user + "登录FTP服务失败!" + e.getMessage());
}
this.ftpClient.setBufferSize(1024 * 2);
this.ftpClient.setDataTimeout(30 * 1000);
return isLogin;
}
public void ftpLogOut() {
if (Objects.nonNull(this.ftpClient) && this.ftpClient.isConnected()) {
try {
boolean reuslt = this.ftpClient.logout();
if (reuslt) {
log.info("成功退出服务器");
}
} catch (IOException e) {
e.printStackTrace();
log.warn("退出FTP服务器异常!" + e.getMessage());
} finally {
try {
this.ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
log.warn("关闭FTP服务器的连接异常!");
}
}
}
}
public void uploadFile(File localFile, String romotUpLoadePath) {
BufferedInputStream inStream = null;
boolean success = false;
try {
this.ftpClient.changeWorkingDirectory(romotUpLoadePath);
inStream = new BufferedInputStream(new FileInputStream(localFile));
log.info(localFile.getName() + "开始上传.....");
success = this.ftpClient.storeFile(localFile.getName(), inStream);
if (Boolean.TRUE.equals(success)) {
log.info(localFile.getName() + "上传成功");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
log.error(localFile + "未找到");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void downLoadFile(String remoteFileName, String localDires,
String remoteDownLoadPath) {
String strFilePath = localDires + remoteFileName;
BufferedOutputStream outStream = null;
boolean success = false;
try {
this.ftpClient.changeWorkingDirectory(remoteDownLoadPath);
outStream = new BufferedOutputStream(new FileOutputStream(strFilePath));
log.info(remoteFileName + "开始下载....");
success = this.ftpClient.retrieveFile(remoteFileName, outStream);
if (Boolean.TRUE.equals(success)) {
log.info(remoteFileName + "成功下载到" + strFilePath);
}
} catch (Exception e) {
e.printStackTrace();
log.error(remoteFileName + "下载失败");
} finally {
if (null != outStream) {
try {
outStream.flush();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (Boolean.FALSE.equals(success)) {
log.error(remoteFileName + "下载失败!!!");
}
}
public void uploadDirectory(String localDirectory,
String remoteDirectoryPath) {
File src = new File(localDirectory);
try {
remoteDirectoryPath = remoteDirectoryPath + src.getName() + File.separator;
boolean makeDirFlag = this.ftpClient.makeDirectory(remoteDirectoryPath);
log.info("localDirectory : " + localDirectory);
log.info("remoteDirectoryPath : " + remoteDirectoryPath);
log.info("src.getName() : " + src.getName());
log.info("remoteDirectoryPath : " + remoteDirectoryPath);
log.info("makeDirFlag : " + makeDirFlag);
} catch (IOException e) {
e.printStackTrace();
log.info(remoteDirectoryPath + "目录创建失败");
}
File[] allFile = src.listFiles();
for (int currentFile = 0; currentFile < allFile.length; currentFile++) {
if (!allFile[currentFile].isDirectory()) {
String srcName = allFile[currentFile].getPath();
uploadFile(new File(srcName), remoteDirectoryPath);
}
}
for (int currentFile = 0; currentFile < allFile.length; currentFile++) {
if (allFile[currentFile].isDirectory()) {
uploadDirectory(allFile[currentFile].getPath().toString(),
remoteDirectoryPath);
}
}
}
public void downLoadDirectory(String localDirectoryPath, String remoteDirectory) {
try {
String fileName = new File(remoteDirectory).getName();
localDirectoryPath = localDirectoryPath + fileName + File.separator;
new File(localDirectoryPath).mkdirs();
FTPFile[] allFile = this.ftpClient.listFiles(remoteDirectory, new Myfilter());
for (FTPFile ftpFile : allFile) {
if (!ftpFile.isDirectory()) {
downLoadFile(ftpFile.getName(), localDirectoryPath, remoteDirectory);
}
}
for (FTPFile ftpFile : allFile) {
if (ftpFile.isDirectory()) {
String remoteDPath = remoteDirectory + File.separator + ftpFile.getName();
downLoadDirectory(localDirectoryPath, remoteDPath);
}
}
} catch (IOException e) {
e.printStackTrace();
log.info("下载文件夹失败");
}
}
3、编写测试类
FtpClientUtil ftp = new FtpClientUtil("127.0.0.1", 8080, "root", "root");
ftp.ftpLogin();
ftp.downLoadDirectory("d://file//", "/");
ftp.ftpLogOut();