日志管理关闭ftp连接

下载文件代码修改
This commit is contained in:
qiaoqinzheng 2023-06-15 14:27:08 +08:00
parent 6daca4ca17
commit e43fec651b
2 changed files with 49 additions and 33 deletions

View File

@ -8,8 +8,11 @@ import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -60,14 +63,15 @@ public class FTPUtil {
return ftp; return ftp;
} }
public InputStream downloadFTPFile(String localPath, String fileName){ public void downloadFTPFile(String localPath, String fileName, HttpServletResponse response) {
InputStream in = null; InputStream in = null;
//传输模式 ServletOutputStream out = null;
try {
FTPClient ftpClient = this.LoginFTP(); FTPClient ftpClient = this.LoginFTP();
if (Objects.isNull(ftpClient)){ if (Objects.isNull(ftpClient)){
throw new RuntimeException("ftp连接失败!"); throw new RuntimeException("ftp连接失败!");
} }
//传输模式
try {
List<String> paths = Arrays.asList(localPath.split("/")); List<String> paths = Arrays.asList(localPath.split("/"));
if (CollectionUtils.isNotEmpty(paths)){ if (CollectionUtils.isNotEmpty(paths)){
for (String workPath:paths) { for (String workPath:paths) {
@ -88,10 +92,41 @@ public class FTPUtil {
} }
} }
} }
//重置响应信息
response.reset();
//设置响应类型
response.setContentType("application/download");
//解决中文不能生成文件
response.setHeader("Content-Disposition", "attachment; fileName=" + URLEncoder.encode(fileName,"UTF-8"));
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
//获取输出流
out = response.getOutputStream();
//声明一个长度参数
int len;
//声明字节数组
byte[] bytes = new byte[1024];
//判断如果输入流的字节长度不等于-1进行字节数组内容的读取
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
out.flush();
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
if (ftpClient != null){
ftpClient.disconnect();
}
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return in; }
} }
} }

View File

@ -62,6 +62,9 @@ public class LogManageController {
} }
result.addAll(list); result.addAll(list);
} }
if (ftpClient != null){
ftpClient.disconnect();
}
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -178,6 +181,9 @@ public class LogManageController {
} }
} }
} }
if (ftpClient != null){
ftpClient.disconnect();
}
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -186,36 +192,11 @@ public class LogManageController {
@PostMapping("downloadFile") @PostMapping("downloadFile")
@ApiOperation(value = "ftp文件下载", notes = "ftp文件下载") @ApiOperation(value = "ftp文件下载", notes = "ftp文件下载")
public void downloadFile(String localPath, String fileName, HttpServletResponse response) throws IOException { public void downloadFile(String localPath, String fileName, HttpServletResponse response) {
if (localPath.contains(fileName)){ if (localPath.contains(fileName)){
localPath=localPath.substring(0,localPath.indexOf(fileName)-1); localPath=localPath.substring(0,localPath.indexOf(fileName)-1);
} }
//重置响应信息 ftpUtil.downloadFTPFile(localPath, fileName,response);
response.reset();
//设置响应类型
response.setContentType("application/download");
//解决中文不能生成文件
response.setHeader("Content-Disposition", "attachment; fileName=" + URLEncoder.encode(fileName,"UTF-8"));
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
//获取文件的输入流
InputStream in = ftpUtil.downloadFTPFile(localPath, fileName);
//获取输出流
ServletOutputStream out = response.getOutputStream();
//声明一个长度参数
int len;
//声明字节数组
byte[] bytes = new byte[1024];
//判断如果输入流的字节长度不等于-1进行字节数组内容的读取
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
out.flush();
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} }
} }