文档库--过滤文件预览

This commit is contained in:
zhn
2022-03-21 16:24:07 +08:00
parent e1c48cf63a
commit 8b753f4a66
2 changed files with 68 additions and 121 deletions
@@ -90,6 +90,7 @@ public class ShiroConfig {
filterChainDefinitionMap.put("/auth/2step-code", "anon");//登录验证码 filterChainDefinitionMap.put("/auth/2step-code", "anon");//登录验证码
filterChainDefinitionMap.put("/sys/common/static/**", "anon");//图片预览 &下载文件不限制token filterChainDefinitionMap.put("/sys/common/static/**", "anon");//图片预览 &下载文件不限制token
filterChainDefinitionMap.put("/sys/common/pdf/**", "anon");//pdf预览 filterChainDefinitionMap.put("/sys/common/pdf/**", "anon");//pdf预览
filterChainDefinitionMap.put("/sys/common/download/**", "anon");//文件预览
filterChainDefinitionMap.put("/generic/**", "anon");//pdf预览需要文件 filterChainDefinitionMap.put("/generic/**", "anon");//pdf预览需要文件
filterChainDefinitionMap.put("/", "anon"); filterChainDefinitionMap.put("/", "anon");
filterChainDefinitionMap.put("/doc.html", "anon"); filterChainDefinitionMap.put("/doc.html", "anon");
@@ -180,73 +180,73 @@ public class CommonController {
* @param request * @param request
* @param response * @param response
*/ */
@GetMapping(value = "/download/{id}") // @GetMapping(value = "/download/{id}")
public void view(@PathVariable String id,HttpServletRequest request, HttpServletResponse response) { // public void view(@PathVariable String id,HttpServletRequest request, HttpServletResponse response) {
// 查询数据表数据是否存在 // // 查询数据表数据是否存在
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>(); // LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(OSSFile::getId,id); // queryWrapper.eq(OSSFile::getId,id);
OSSFile ossFile = ossFileService.getOne(queryWrapper); // OSSFile ossFile = ossFileService.getOne(queryWrapper);
if( null == ossFile){ // if( null == ossFile){
throw new JeroBootException("文件不存在.."); // throw new JeroBootException("文件不存在..");
} // }
String fileUrl = ossFile.getUrl(); // String fileUrl = ossFile.getUrl();
InputStream inputStream = null; // InputStream inputStream = null;
OutputStream outputStream = null; // OutputStream outputStream = null;
try { // try {
String fileName = ""; // String fileName = "";
if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ // if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
//本地下载 // //本地下载
String filePath = uploadpath + File.separator + fileUrl; // String filePath = uploadpath + File.separator + fileUrl;
File file = new File(filePath); // File file = new File(filePath);
if(!file.exists()){ // if(!file.exists()){
response.setStatus(404); // response.setStatus(404);
throw new RuntimeException("文件不存在.."); // throw new RuntimeException("文件不存在..");
} // }
// 文件名称 // // 文件名称
fileName = file.getName(); // fileName = file.getName();
inputStream = new BufferedInputStream(new FileInputStream(filePath)); // inputStream = new BufferedInputStream(new FileInputStream(filePath));
}else if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){ // }else if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
// minio 下载 // // minio 下载
// 通过MinioUtil查询时 只需要桶后面的路径 // // 通过MinioUtil查询时 只需要桶后面的路径
String minioUrl = MinioUtil.getMinioUrl(); // String minioUrl = MinioUtil.getMinioUrl();
// Linux/unix 系统下文件路径分隔符为"/" 获取minio与存储桶的路径 // // Linux/unix 系统下文件路径分隔符为"/" 获取minio与存储桶的路径
minioUrl = minioUrl + MinioUtil.getBucketName() + "/"; // minioUrl = minioUrl + MinioUtil.getBucketName() + "/";
String url = fileUrl.replace(minioUrl, ""); // String url = fileUrl.replace(minioUrl, "");
// 文件名称 // // 文件名称
fileName = ossFile.getFileName(); // fileName = ossFile.getFileName();
inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url); // inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
} // }
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1")); // response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
// response.setContentType("application/force-download");// 设置强制下载不打开 //// response.setContentType("application/force-download");// 设置强制下载不打开
outputStream = response.getOutputStream(); // outputStream = response.getOutputStream();
byte[] buf = new byte[1024]; // byte[] buf = new byte[1024];
int len; // int len;
while ((len = inputStream.read(buf)) > 0) { // while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len); // outputStream.write(buf, 0, len);
} // }
response.flushBuffer(); // response.flushBuffer();
} catch (IOException e) { // } catch (IOException e) {
log.error("预览文件失败" + e.getMessage()); // log.error("预览文件失败" + e.getMessage());
response.setStatus(404); // response.setStatus(404);
e.printStackTrace(); // e.printStackTrace();
} finally { // } finally {
if (inputStream != null) { // if (inputStream != null) {
try { // try {
inputStream.close(); // inputStream.close();
} catch (IOException e) { // } catch (IOException e) {
log.error(e.getMessage(), e); // log.error(e.getMessage(), e);
} // }
} // }
if (outputStream != null) { // if (outputStream != null) {
try { // try {
outputStream.close(); // outputStream.close();
} catch (IOException e) { // } catch (IOException e) {
log.error(e.getMessage(), e); // log.error(e.getMessage(), e);
} // }
} // }
} // }
//
} // }
/** /**
* 下载文件 * 下载文件
* *
@@ -307,60 +307,6 @@ public class CommonController {
} }
} }
}
@GetMapping(value = "/fileView/{id}")
public void fileView(String id,HttpServletRequest request, HttpServletResponse response) {
// 查询数据表数据是否存在
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(OSSFile::getId,id);
OSSFile ossFile = ossFileService.getOne(queryWrapper);
if( null == ossFile){
throw new JeroBootException("文件不存在..");
}
InputStream inputStream = null;
OutputStream outputStream = null;
try {
String fileName = "";
//本地下载
String filePath = ossFile.getUrl();
File file = new File(filePath);
if(!file.exists()){
response.setStatus(404);
throw new RuntimeException("文件不存在..");
}
// 文件名称
fileName = file.getName();
inputStream = new BufferedInputStream(new FileInputStream(filePath));
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
response.setContentType("application/force-download");// 设置强制下载不打开
outputStream = response.getOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
response.flushBuffer();
} catch (IOException e) {
log.error("文件下载失败" + e.getMessage());
response.setStatus(404);
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
} }
/** /**
* 文件预览 * 文件预览