package com.newlinker.scs.utils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
/**
* @author cyl
* @time 2022/6/14
*/
@Component
public class FtpUtils {
@Value("${ftp.username}")
private String fUsername;
private static String FTP_USER;
@Value("${ftp.password}")
private String fPassword;
private static String FTP_PASSWORD;
@Value("${ftp.host}")
private String fHost;
private static String FTP_IP;
@Value("${ftp.port}")
private Integer fPort;
private static Integer FTP_PORT;
@Value("${ftp.charset.iso}")
private String fCharsetIso;
private static String CHARSET_ISO;
@PostConstruct
public void init(){
FTP_USER=fUsername;
FTP_PASSWORD=fPassword;
FTP_IP=fHost;
FTP_PORT=fPort;
CHARSET_ISO=fCharsetIso;
}
/**
* 下载指定Ftp文件夹的全部文件
* @param folderPath ftp文件夹路径
* @param localPath 下载本地磁盘路径
* @return
*/
public static Boolean downLoadFolder(String folderPath,String localPath){
FTPClient ftp = getFTPClient();
try {
if(ftp == null){
return false;
}
ftp.enterLocalPassiveMode();
//切换到文件目录
ftp.changeWorkingDirectory(folderPath);
//获取文件集合
FTPFile[] files = ftp.listFiles();
for(FTPFile file : files){
if(file.isFile()){
try (OutputStream out = new FileOutputStream(localPath + "\\\\" + file.getName())){
ftp.retrieveFile(new String(file.getName().getBytes(),CHARSET_ISO),out);
out.flush();
}
}
}
}catch (Exception e){
e.printStackTrace();
return false;
}finally {
closeFtp(ftp);
}
return true;
}
/**
* 文件下载
* @param parentPath 文件在ftp上级路径
* @param ftpFileName 文件在ftp名称
* @param localPath 下载本地磁盘路径
* @return
*/
public static Boolean downLoad(String parentPath,String ftpFileName,String localPath){
FTPClient ftp = getFTPClient();
try {
if(ftp == null){
return false;
}
ftp.enterLocalPassiveMode();
parentPath = parentPath + "/" +ftpFileName;
System.out.println(parentPath);
//根据文件名称获取输入流
try (InputStream in = ftp.retrieveFileStream(new String(parentPath.getBytes(),CHARSET_ISO))){
//写入本地文件目录
File dest = new File(localPath+"\\"+ftpFileName);
if (!dest.getParentFile().exists()) {
System.out.println("创建文件夹.................");
dest.getParentFile().mkdirs();
}
Files.copy(in, dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
return true;
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeFtp(ftp);
}
return false;
}
/**
* 文件上传
* @param filePath 文件路径
* @param ftpPath ftp路径
* @return
*/
public static Boolean upload(String filePath,String ftpPath){
return upload(new File(filePath),ftpPath);
}
/**
* 文件上传
* @param file 文件对象
* @param ftpPath ftp路径
* @return
*/
public static Boolean upload(File file,String ftpPath){
FTPClient ftp = getFTPClient();
try {
if(ftp == null){
return false;
}
//设置被动模式
ftp.enterLocalPassiveMode();
//二进制传输
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//如果ftp目录不存在则创建目录
if(!ftp.changeWorkingDirectory(ftpPath)){
ftp.makeDirectory(ftpPath);
//改变工作目录
ftp.changeWorkingDirectory(ftpPath);
}
//文件名
String fileName = file.getName();
//上传文件
if(ftp.storeFile(new String(fileName.getBytes(),CHARSET_ISO),new FileInputStream(file))){
return true;
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeFtp(ftp);
}
return false;
}
/**
* 获取Ftp客户端对象
* @return
*/
private static FTPClient getFTPClient(){
try {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(FTP_IP,FTP_PORT);
//连接超时时间
ftpClient.setConnectTimeout(2 * 60 * 1000);
//传输超时时间
ftpClient.setDataTimeout(2 * 60 * 1000);
ftpClient.setControlEncoding("utf-8");
//用户名密码登录
ftpClient.login(FTP_USER,FTP_PASSWORD);
//校验响应码
if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
ftpClient.disconnect();
return null;
}
return ftpClient;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 关闭Ftp客户端
* @param ftp
*/
private static void closeFtp(FTPClient ftp){
try {
if(ftp != null){
ftp.logout();
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(ftp != null){
ftp.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}