feat: 不带水印下载接口
This commit is contained in:
+45
-30
@@ -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;
|
||||
@@ -50,7 +51,7 @@ public class CommonController {
|
||||
private ISysBaseAPI sysBaseAPI;
|
||||
|
||||
@Resource
|
||||
private IOSSFileService ossFileService;
|
||||
private IOSSFileService ossFileService;
|
||||
|
||||
@Resource
|
||||
private SysWaterMarkUtil sysWaterMarkUtil;
|
||||
@@ -61,29 +62,30 @@ public class CommonController {
|
||||
/**
|
||||
* 本地:local minio:minio 阿里:alioss
|
||||
*/
|
||||
@Value(value="${jero.uploadType}")
|
||||
@Value(value = "${jero.uploadType}")
|
||||
private String uploadType;
|
||||
/**
|
||||
* 文件后缀黑名单
|
||||
*/
|
||||
@Value(value="${jero.fileSuffixLimits}")
|
||||
@Value(value = "${jero.fileSuffixLimits}")
|
||||
private String[] fileSuffixLimits;
|
||||
|
||||
private static final String UTF8 ="UTF-8";
|
||||
private static final String UTF8 = "UTF-8";
|
||||
|
||||
private static final String FILE_VIEW_ERROR = "预览文件失败";
|
||||
|
||||
/**
|
||||
* @Author 政辉
|
||||
* @return
|
||||
* @Author 政辉
|
||||
*/
|
||||
@GetMapping("/403")
|
||||
public Result<String> noauth() {
|
||||
public Result<String> noauth() {
|
||||
return Result.error("没有权限,请联系管理员授权");
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传统一方法
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @return
|
||||
@@ -102,27 +104,27 @@ public class CommonController {
|
||||
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
||||
// 获取上传文件对象
|
||||
MultipartFile file = multipartRequest.getFile("file");
|
||||
if(file==null) {
|
||||
if (file == null) {
|
||||
return result;
|
||||
}
|
||||
// 文件类型是否处于黑名单
|
||||
if(CommonUtils.limitFileSuffix(file.getOriginalFilename(),fileSuffixLimits)){
|
||||
if (CommonUtils.limitFileSuffix(file.getOriginalFilename(), fileSuffixLimits)) {
|
||||
result.setMessage("该文件类型不允许上传");
|
||||
result.setSuccess(false);
|
||||
result.setCode(0);
|
||||
return result;
|
||||
}
|
||||
if(oConvertUtils.isEmpty(bizPath)){
|
||||
bizPath = "";
|
||||
if (oConvertUtils.isEmpty(bizPath)) {
|
||||
bizPath = "";
|
||||
}
|
||||
if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
|
||||
if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)) {
|
||||
// 本地上传
|
||||
savePath = CommonUtils.uploadLocal(file,bizPath,uploadpath);
|
||||
}else{
|
||||
savePath = CommonUtils.uploadLocal(file, bizPath, uploadpath);
|
||||
} else {
|
||||
// minio上传
|
||||
savePath = CommonUtils.upload(file, bizPath, uploadType);
|
||||
}
|
||||
if(oConvertUtils.isNotEmpty(savePath)){
|
||||
if (oConvertUtils.isNotEmpty(savePath)) {
|
||||
//上传成功 进行数据库存储
|
||||
OSSFile ossFile = new OSSFile();
|
||||
// 文件名
|
||||
@@ -134,7 +136,7 @@ public class CommonController {
|
||||
result.setMessage(savePath);
|
||||
result.setResult(ossFile);
|
||||
result.setSuccess(true);
|
||||
}else {
|
||||
} else {
|
||||
result.setMessage("上传失败!");
|
||||
result.setSuccess(false);
|
||||
}
|
||||
@@ -144,24 +146,24 @@ public class CommonController {
|
||||
/**
|
||||
* 下载文件
|
||||
*
|
||||
* @param id 传入文件id
|
||||
* @param id 传入文件id
|
||||
* @param request
|
||||
* @param response
|
||||
*/
|
||||
@GetMapping(value = "/download/{id}")
|
||||
public void download(@PathVariable String id,HttpServletRequest request, HttpServletResponse response) {
|
||||
downloadAndView(id, response);
|
||||
public void download(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) {
|
||||
downloadAndViewWithWaterMark(id, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 预览图片
|
||||
*
|
||||
* @param id 传入文件id
|
||||
* @param id 传入文件id
|
||||
* @param request
|
||||
* @param response
|
||||
*/
|
||||
@GetMapping(value = "/view/{id}")
|
||||
public void view(@PathVariable String id,HttpServletRequest request, HttpServletResponse response) {
|
||||
public void view(@PathVariable String id, HttpServletRequest request, HttpServletResponse 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<>();
|
||||
@@ -277,14 +291,14 @@ public class CommonController {
|
||||
) {
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
if(inputStream==null) {
|
||||
if (inputStream == null) {
|
||||
return;
|
||||
}
|
||||
while ((len = inputStream.read(buf)) > 0) {
|
||||
outputStream.write(buf, 0, len);
|
||||
}
|
||||
response.flushBuffer();
|
||||
} catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
response.setStatus(404);
|
||||
e.printStackTrace();
|
||||
@@ -299,9 +313,9 @@ public class CommonController {
|
||||
}
|
||||
|
||||
/**
|
||||
* @功能:pdf预览Iframe
|
||||
* @param modelAndView
|
||||
* @return
|
||||
* @功能:pdf预览Iframe
|
||||
*/
|
||||
@RequestMapping("/pdf/pdfPreviewIframe")
|
||||
public ModelAndView pdfPreviewIframe(ModelAndView modelAndView) {
|
||||
@@ -310,8 +324,9 @@ public class CommonController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 把指定URL后的字符串全部截断当成参数
|
||||
* 这么做是为了防止URL中包含中文或者特殊字符(/等)时,匹配不了的问题
|
||||
* 把指定URL后的字符串全部截断当成参数
|
||||
* 这么做是为了防止URL中包含中文或者特殊字符(/等)时,匹配不了的问题
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@@ -344,7 +359,7 @@ public class CommonController {
|
||||
headers.set("X-Access-Token", token);
|
||||
// 发送请求
|
||||
String httpURL = URLDecoder.decode(url, UTF8);
|
||||
ResponseEntity<String> response = RestUtil.request(httpURL, method, headers , variables, params, String.class);
|
||||
ResponseEntity<String> response = RestUtil.request(httpURL, method, headers, variables, params, String.class);
|
||||
// 封装返回结果
|
||||
Result<Object> result = new Result<>();
|
||||
int statusCode = response.getStatusCodeValue();
|
||||
@@ -391,18 +406,18 @@ public class CommonController {
|
||||
public void view(HttpServletRequest request, HttpServletResponse response) {
|
||||
// ISO-8859-1 ==> UTF-8 进行编码转换
|
||||
String imgPath = extractPathFromPattern(request);
|
||||
if(oConvertUtils.isEmpty(imgPath) || "null".equals(imgPath)){
|
||||
if (oConvertUtils.isEmpty(imgPath) || "null".equals(imgPath)) {
|
||||
return;
|
||||
}
|
||||
imgPath = imgPath.replace("..", "").replace("../","");
|
||||
imgPath = imgPath.replace("..", "").replace("../", "");
|
||||
if (imgPath.endsWith(",")) {
|
||||
imgPath = imgPath.substring(0, imgPath.length() - 1);
|
||||
}
|
||||
String filePath = uploadpath + File.separator + imgPath;
|
||||
File file = new File(filePath);
|
||||
if(!file.exists()){
|
||||
if (!file.exists()) {
|
||||
response.setStatus(404);
|
||||
throw new JeroBootException("文件["+imgPath+"]不存在..");
|
||||
throw new JeroBootException("文件[" + imgPath + "]不存在..");
|
||||
}
|
||||
response.setContentType("application/force-download");// 设置强制下载不打开
|
||||
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
||||
|
||||
-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