文档库--预览

This commit is contained in:
zhn
2022-03-21 15:08:07 +08:00
parent 18f3fb09e4
commit 6d880a638b
@@ -307,6 +307,60 @@ 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);
}
}
}
}
/**
* 文件预览