依赖::
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.14</version>
</dependency>
代码:
import cn.hutool.core.io.FileUtil;
import com.xrp.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.util.regex.Pattern;
/**
* @author:zhengwz
* @desc:
* @date:2021/12/05
**/
@Slf4j
public class FTPUtils {
private FTPClient ftpClient = new FTPClient();
/**
* ftp 文件下载
* @param userName 用户名
* @param passWord 密码
* @param domain 域名
* @param ftpDir ftp文件存放的目录
* @param localDir 文件存放的目录
* @return
* @throws IOException
*/
public void downloadFile(String userName, String passWord, String domain,
String pattern,String ftpDir,String localDir) throws IOException {
try {
File dir = new File(localDir);
if (!dir.exists()) {
dir.mkdirs();
}
// 建立连接
connectToServer(userName,passWord,domain);
ftpClient.enterLocalPassiveMode();
// 设置传输二进制文件
setFileType(FTP.BINARY_FILE_TYPE);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
throw new IOException("failed to connect to the FTP Server:" + domain);
}
//进入文件目录
ftpClient.changeWorkingDirectory(ftpDir);
FTPFile[] ftpFiles = ftpClient.listFiles();
if(ftpFiles != null ){
for(FTPFile file : ftpFiles){
if(file.isFile()){
String ftpFileName = file.getName();
boolean isMatch = Pattern.matches(pattern, ftpFileName);
if(isMatch){
// ftp文件获取文件
InputStream in = ftpClient.retrieveFileStream(ftpFileName);
String localFilePath = localDir + File.separator + ftpFileName;
FileUtil.writeFromStream(in, localFilePath);
ftpClient.getReply();
if (in == null) {
throw new BusinessException("ftp "+ftpFileName + " not exists!");
}
}
}
}
}
} catch (Exception e) {
throw e;
}finally {
closeConnect();
}
}
private void setFileType(int fileType) {
try {
ftpClient.setFileType(fileType);
} catch (Exception e) {
throw new RuntimeException("ftp设置传输文件的类型时失败!");
}
}
public void closeConnect() {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (Exception e) {
throw new RuntimeException("ftp连接关闭失败!");
}
}
private void connectToServer(String userName,String passWord,String domain) {
try {
if (null != domain && !domain.isEmpty()) {//如果通过域名登陆则通过此方法,如果通过IP,port登陆则添加到else方法中
ftpClient.setDefaultTimeout(10 * 60 * 1000);
// 设置连接超时时间
ftpClient.setConnectTimeout(10 * 60 * 1000);
// 设置数据超时时间
ftpClient.setDataTimeout(10 * 60 * 1000);
ftpClient .connect(domain);
// socket连接,设置socket连接超时时间
ftpClient.setSoTimeout(10 * 60 * 1000);
ftpClient .setControlEncoding("UTF-8");
FTPClientConfig ftpConfig = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
ftpConfig.setServerLanguageCode("zh");
ftpClient .login(userName, passWord);
int reply = 0;
reply = ftpClient .getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
log.info("域名方式登录成功!");
} else {
throw new RuntimeException("远程FTP登陆失败,请重新审核");
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}