feat:FTPDownLoadFile
This commit is contained in:
parent
f6d3191637
commit
eb35a181f6
|
@ -1,9 +1,12 @@
|
|||
package org.jeecg.common.util;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
@ -11,6 +14,8 @@ 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 org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.common.constant.Prompt;
|
||||
import org.jeecg.common.constant.SymbolConstant;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
|
@ -195,6 +200,58 @@ public class FTPUtil {
|
|||
}
|
||||
}
|
||||
|
||||
public void downloadFTPFile(String localPath, HttpServletResponse response) {
|
||||
// 连接FTP
|
||||
FTPClient ftpClient = this.LoginFTP();
|
||||
// 判断FTP是否连接成功
|
||||
if (Objects.isNull(ftpClient)){
|
||||
throw new RuntimeException(Prompt.FTP_ERR);
|
||||
}
|
||||
|
||||
OutputStream outputStream = null;
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
// 判断FTP服务器上是否存在此文件
|
||||
String[] files = ftpClient.listNames(localPath);
|
||||
if (ArrayUtil.isEmpty(files)) return;
|
||||
|
||||
// 存在多个文件名表示此路径为目录而非文件
|
||||
if (ArrayUtil.length(files) > 1) return;
|
||||
|
||||
// 获取文件名
|
||||
String fileName = FileUtil.getName(localPath);
|
||||
|
||||
// 在当前工作路径下读取文件
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||||
// 设置编码,当文件中存在中文且上传后文件乱码时可使用此配置项
|
||||
ftpClient.setControlEncoding(encoding);
|
||||
ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
|
||||
inputStream = ftpClient.retrieveFileStream(localPath);
|
||||
outputStream = ExportUtil.stream(response, fileName);
|
||||
|
||||
// 缓冲区大小
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead;
|
||||
|
||||
// 将文件输出流写入到输出流中
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
try {
|
||||
if (ObjectUtil.isNotNull(inputStream))inputStream.close();
|
||||
if (ObjectUtil.isNotNull(outputStream))outputStream.close();
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查目录是否存在,不存在则创建,支持递归创建
|
||||
* @param path 目录路径
|
||||
|
|
|
@ -45,10 +45,6 @@ public class LogManageController {
|
|||
@PostMapping("downloadFile")
|
||||
@ApiOperation(value = "ftp文件下载", notes = "ftp文件下载")
|
||||
public void downloadFile(String localPath, String fileName, HttpServletResponse response) {
|
||||
if (localPath.contains(fileName)){
|
||||
localPath=localPath.substring(0,localPath.indexOf(fileName)-1);
|
||||
ftpUtil.downloadFTPFile(localPath, response);
|
||||
}
|
||||
ftpUtil.downloadFTPFile(localPath, fileName,response);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user