feat: 不带水印下载接口
This commit is contained in:
+18
-3
@@ -12,6 +12,7 @@ import com.jero.common.util.*;
|
||||
import com.jero.modules.oss.entity.OSSFile;
|
||||
import com.jero.modules.oss.service.IOSSFileService;
|
||||
import com.jero.modules.system.util.SysWaterMarkUtil;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -74,8 +75,8 @@ public class CommonController {
|
||||
private static final String FILE_VIEW_ERROR = "预览文件失败";
|
||||
|
||||
/**
|
||||
* @Author 政辉
|
||||
* @return
|
||||
* @Author 政辉
|
||||
*/
|
||||
@GetMapping("/403")
|
||||
public Result<String> noauth() {
|
||||
@@ -84,6 +85,7 @@ public class CommonController {
|
||||
|
||||
/**
|
||||
* 文件上传统一方法
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @return
|
||||
@@ -150,7 +152,7 @@ public class CommonController {
|
||||
*/
|
||||
@GetMapping(value = "/download/{id}")
|
||||
public void download(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) {
|
||||
downloadAndView(id, response);
|
||||
downloadAndViewWithWaterMark(id, response);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -229,6 +231,18 @@ public class CommonController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载不带水印的文件
|
||||
*
|
||||
* @param id 传入文件id
|
||||
* @param response
|
||||
*/
|
||||
@ApiOperation(value = "下载不带水印的文件", notes = "下载不带水印的文件")
|
||||
@GetMapping(value = "/downloadWithoutWaterMark/{id}")
|
||||
public void downloadWithoutWaterMark(@PathVariable String id, HttpServletResponse response) {
|
||||
downloadAndView(id, response);
|
||||
}
|
||||
|
||||
private void downloadAndViewWithWaterMark(@PathVariable String id, HttpServletResponse response) {
|
||||
// 查询数据表数据是否存在
|
||||
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
|
||||
@@ -299,9 +313,9 @@ public class CommonController {
|
||||
}
|
||||
|
||||
/**
|
||||
* @功能:pdf预览Iframe
|
||||
* @param modelAndView
|
||||
* @return
|
||||
* @功能:pdf预览Iframe
|
||||
*/
|
||||
@RequestMapping("/pdf/pdfPreviewIframe")
|
||||
public ModelAndView pdfPreviewIframe(ModelAndView modelAndView) {
|
||||
@@ -312,6 +326,7 @@ public class CommonController {
|
||||
/**
|
||||
* 把指定URL后的字符串全部截断当成参数
|
||||
* 这么做是为了防止URL中包含中文或者特殊字符(/等)时,匹配不了的问题
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
|
||||
-71
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user