feat: 不带水印下载接口

This commit is contained in:
2024-01-02 10:07:26 +08:00
parent b77af10c65
commit d4e6b778af
2 changed files with 45 additions and 101 deletions
@@ -86,75 +86,4 @@ public class LawsCommonController {
return Result.OK(lawsCommonService.getSplitsByStandardNumber(standardNumber));
}
/**
* 下载带水印的PDF文件
*
* @param id 传入文件id
* @param response
*/
@ApiOperation(value="PDF带水印下载", notes="PDF带水印下载")
@GetMapping(value = "/downloadWithWaterMark/{id}")
public void downloadWithWaterMark(@PathVariable String id, HttpServletResponse response) {
// 查询数据表数据是否存在
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(OSSFile::getId, id);
OSSFile ossFile = ossFileService.getOne(queryWrapper);
if (null == ossFile) {
throw new JeroBootException("文件不存在..");
}
String fileUrl = ossFile.getUrl();
// minio 下载
// 通过MinioUtil查询时 只需要桶后面的路径
String minioUrl = MinioUtil.getMinioUrl();
// Linux/unix 系统下文件路径分隔符为"/" 获取minio与存储桶的路径
minioUrl = minioUrl + MinioUtil.getBucketName() + "/";
String url = fileUrl.replace(minioUrl, "");
// 文件名称
String fileName = ossFile.getFileName();
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
// 设置强制下载不打开
response.setContentType("application/force-download");
// 然后在您的controller方法中:
if ("pdf".equalsIgnoreCase(FileUtil.extName(fileName))) {
try (InputStream originalInputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
InputStream watermarkedInputStream = waterMarkUtil.addWatermarkToPdf(originalInputStream);
OutputStream outputStream = response.getOutputStream()) {
if (originalInputStream == null) {
return;
}
byte[] buf = new byte[1024];
int len;
while ((len = watermarkedInputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
response.flushBuffer();
} catch (Exception e) {
log.error(e.getMessage());
response.setStatus(404);
e.printStackTrace();
}
} else {
try (InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
OutputStream outputStream = response.getOutputStream()
) {
byte[] buf = new byte[1024];
int len;
if(inputStream==null) {
return;
}
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
response.flushBuffer();
} catch (Exception e){
log.error(e.getMessage());
response.setStatus(404);
e.printStackTrace();
}
}
}
}