feat:FTP连接相关代码提交,返回内容实体类提交
This commit is contained in:
parent
a1f9d02188
commit
a74d96bc51
|
@ -0,0 +1,122 @@
|
||||||
|
package org.jeecg.common.util;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||||
|
import org.apache.commons.net.ftp.FTPClient;
|
||||||
|
import org.apache.commons.net.ftp.FTPFile;
|
||||||
|
import org.apache.commons.net.ftp.FTPReply;
|
||||||
|
import org.jeecg.modules.entity.LogManage;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class FTPUtil {
|
||||||
|
|
||||||
|
@Value("${ftp.host}")
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
@Value("${ftp.port}")
|
||||||
|
private Integer port;
|
||||||
|
|
||||||
|
@Value("${ftp.userName}")
|
||||||
|
private String userName;
|
||||||
|
|
||||||
|
@Value("${ftp.password}")
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
@Value("${ftp.encoding}")
|
||||||
|
private String encoding;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录ftp
|
||||||
|
* @param workPath
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public FTPClient LoginFTP(String workPath){
|
||||||
|
//声明FTP客户端
|
||||||
|
FTPClient ftp = new FTPClient();
|
||||||
|
try {
|
||||||
|
//连接
|
||||||
|
ftp.connect(host, port);
|
||||||
|
//登录
|
||||||
|
ftp.login(userName, password);
|
||||||
|
//判断是否连接成功
|
||||||
|
int reply = ftp.getReplyCode();
|
||||||
|
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||||
|
ftp.disconnect();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||||||
|
//切换工作文件路径
|
||||||
|
ftp.changeWorkingDirectory(workPath);
|
||||||
|
// 设置编码,当文件中存在中文且上传后文件乱码时可使用此配置项
|
||||||
|
ftp.setControlEncoding(encoding);
|
||||||
|
// 切换为本地被动模式,可以解决FTP上传后文件为空的问题,但需要服务器将FTP服务添加至防火墙白名单
|
||||||
|
ftp.enterLocalPassiveMode();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return ftp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 遍历查询当前路径下的文件夹信息
|
||||||
|
* @param ftp
|
||||||
|
* @param list
|
||||||
|
* @param filePath 以"/"开始和结束
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<LogManage> findDirectory(FTPClient ftp,List<LogManage> list,Integer parentNum,String filePath){
|
||||||
|
try {
|
||||||
|
ftp.changeWorkingDirectory(filePath);
|
||||||
|
List<FTPFile> ftpFiles = Arrays.asList(ftp.listDirectories());
|
||||||
|
if (CollectionUtils.isNotEmpty(ftpFiles)){
|
||||||
|
int num =1;
|
||||||
|
for (FTPFile file : ftpFiles) {
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
LogManage logManage = new LogManage();
|
||||||
|
logManage.setName(file.getName());
|
||||||
|
logManage.setOrderNum(num);
|
||||||
|
logManage.setParentNum(parentNum);
|
||||||
|
logManage.setPath(filePath+ file.getName() + "/");
|
||||||
|
list.add(logManage);
|
||||||
|
num++;
|
||||||
|
// 需要加此判断。否则,ftp默认将‘项目文件所在目录之下的目录(./)’与‘项目文件所在目录向上一级目录下的目录(../)’都纳入递归,这样下去就陷入一个死循环了。需将其过滤掉。
|
||||||
|
if (!".".equals(file.getName()) && !"..".equals(file.getName())) {
|
||||||
|
findDirectory(ftp,list,num,filePath + file.getName() + "/");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InputStream downloadFTPFile(String localPath, String fileName){
|
||||||
|
InputStream in = null;
|
||||||
|
FTPClient ftpClient = this.LoginFTP(localPath);
|
||||||
|
//传输模式
|
||||||
|
try {
|
||||||
|
ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
|
||||||
|
List<FTPFile> ftpFiles = Arrays.asList(ftpClient.listFiles());
|
||||||
|
if (CollectionUtils.isNotEmpty(ftpFiles)){
|
||||||
|
for (FTPFile ftpFile:ftpFiles) {
|
||||||
|
if (ftpFile.getName().equals(fileName)){
|
||||||
|
in = ftpClient.retrieveFileStream(new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package org.jeecg.modules.entity;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class FileInfo {
|
||||||
|
|
||||||
|
private String fileName;
|
||||||
|
|
||||||
|
private String filePath;
|
||||||
|
|
||||||
|
private String fileSize;
|
||||||
|
|
||||||
|
private String fileDate;
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user