日志管理关闭ftp连接
下载文件代码修改
This commit is contained in:
parent
6daca4ca17
commit
e43fec651b
|
@ -8,8 +8,11 @@ import org.apache.commons.net.ftp.FTPReply;
|
|||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
@ -60,14 +63,15 @@ public class FTPUtil {
|
|||
return ftp;
|
||||
}
|
||||
|
||||
public InputStream downloadFTPFile(String localPath, String fileName){
|
||||
public void downloadFTPFile(String localPath, String fileName, HttpServletResponse response) {
|
||||
InputStream in = null;
|
||||
ServletOutputStream out = null;
|
||||
FTPClient ftpClient = this.LoginFTP();
|
||||
if (Objects.isNull(ftpClient)){
|
||||
throw new RuntimeException("ftp连接失败!");
|
||||
}
|
||||
//传输模式
|
||||
try {
|
||||
FTPClient ftpClient = this.LoginFTP();
|
||||
if (Objects.isNull(ftpClient)){
|
||||
throw new RuntimeException("ftp连接失败!");
|
||||
}
|
||||
List<String> paths = Arrays.asList(localPath.split("/"));
|
||||
if (CollectionUtils.isNotEmpty(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) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -62,6 +62,9 @@ public class LogManageController {
|
|||
}
|
||||
result.addAll(list);
|
||||
}
|
||||
if (ftpClient != null){
|
||||
ftpClient.disconnect();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
@ -178,6 +181,9 @@ public class LogManageController {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (ftpClient != null){
|
||||
ftpClient.disconnect();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
@ -186,36 +192,11 @@ public class LogManageController {
|
|||
|
||||
@PostMapping("downloadFile")
|
||||
@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)){
|
||||
localPath=localPath.substring(0,localPath.indexOf(fileName)-1);
|
||||
}
|
||||
//重置响应信息
|
||||
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();
|
||||
}
|
||||
ftpUtil.downloadFTPFile(localPath, fileName,response);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user