预览文件时删除临时文件

This commit is contained in:
chenxudong 2025-04-27 10:58:54 +08:00
parent a04b6047f1
commit 402e60b65f
1 changed files with 6 additions and 3 deletions

View File

@ -1290,12 +1290,13 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
*/ */
@Override @Override
public ResponseEntity<InputStreamResource> preview(String id, HttpServletResponse response, int dataOwnCode) { public ResponseEntity<InputStreamResource> preview(String id, HttpServletResponse response, int dataOwnCode) {
String fileSaveTmpPath = null;
try { try {
EdFileInfo fileInfo = this.baseMapper.selectById(id); EdFileInfo fileInfo = this.baseMapper.selectById(id);
Assert.isTrue(Objects.nonNull(fileInfo), "文件不存在"); Assert.isTrue(Objects.nonNull(fileInfo), "文件不存在");
String fileDbPath = commonService.getDbPath(fileInfo.getFilePath()); String fileDbPath = commonService.getDbPath(fileInfo.getFilePath());
String fileSysPath = commonService.getFileSysPath(fileInfo.getId()); String fileSysPath = commonService.getFileSysPath(fileInfo.getId());
String fileSaveTmpPath = elePropertyConfig.getEleTmpPath() + File.separator + IdUtil.fastSimpleUUID() + "." + fileInfo.getFileType(); fileSaveTmpPath = elePropertyConfig.getEleTmpPath() + File.separator + IdUtil.fastSimpleUUID() + "." + fileInfo.getFileType();
FileUtil.copy(fileSysPath, fileSaveTmpPath, true); FileUtil.copy(fileSysPath, fileSaveTmpPath, true);
EleCommonUtil.decryptFile(fileSaveTmpPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes())); EleCommonUtil.decryptFile(fileSaveTmpPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
@ -1314,7 +1315,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
FileSystemResource fileSystemResource = new FileSystemResource(fileSaveTmpPath); FileSystemResource fileSystemResource = new FileSystemResource(fileSaveTmpPath);
String fileName = fileSystemResource.getFilename(); String fileName = fileSystemResource.getFilename();
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
InputStream inputStream = fileSystemResource.getInputStream();
fileName = Base64.encode(fileName.substring(0, fileName.lastIndexOf("."))); fileName = Base64.encode(fileName.substring(0, fileName.lastIndexOf(".")));
headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate"); headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
headers.add(HttpHeaders.PRAGMA, "no-cache"); headers.add(HttpHeaders.PRAGMA, "no-cache");
@ -1328,11 +1329,13 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
.headers(headers) .headers(headers)
.contentLength(fileSystemResource.contentLength()) .contentLength(fileSystemResource.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream;charset=UTF-8")) .contentType(MediaType.parseMediaType("application/octet-stream;charset=UTF-8"))
.body(new InputStreamResource(fileSystemResource.getInputStream())); .body(new InputStreamResource(inputStream));
} catch (Exception e) { } catch (Exception e) {
String info = StrFormatter.format("文件预览错误文件id是--->{},错误信息--->{}", id, e.getMessage()); String info = StrFormatter.format("文件预览错误文件id是--->{},错误信息--->{}", id, e.getMessage());
log.error(info, e); log.error(info, e);
throw new BizException(info); throw new BizException(info);
} finally {
FileUtil.del(fileSaveTmpPath);
} }
} }