认证-参数项收集-下载pdf无水印

This commit is contained in:
liyawei
2022-10-17 17:22:28 +08:00
parent 2c6f8f1d5a
commit 5a09fefa76
@@ -7,7 +7,6 @@ import com.jero.common.api.vo.Result;
import com.jero.common.constant.enums.CutEnum;
import com.jero.common.exception.JeroBootException;
import com.jero.common.system.api.ISysBaseAPI;
import com.jero.common.system.vo.LoginUser;
import com.jero.common.util.RestUtil;
import com.jero.common.util.TokenUtils;
import com.jero.common.util.oConvertUtils;
@@ -19,7 +18,6 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
@@ -27,12 +25,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.HandlerMapping;
@@ -41,12 +34,7 @@ import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -382,12 +370,13 @@ public class CommonController {
fileName = ossFile.getFileName();
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
InputStream download = CosBootUtil.download(filePath);
File file = new File(filePath);
if(file.getName().endsWith(".pdf") || file.getName().endsWith(".PDF")){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(new Date());
String waterContent = userName+" "+currentTime;
InputStream download = CosBootUtil.download(filePath);
newFile = PDFUtils.PDFWatermark(download,uploadpath,file.getName(),waterContent);
inputStream = new FileInputStream(newFile.getPath());
}else{
@@ -432,6 +421,83 @@ public class CommonController {
}
}
/**
* word,excel预览 cos -pdf无水印
*
* @param id 传入文件id
* @param request
* @param response
*/
@GetMapping(value = "/downLoadFileNOMark")
public void downLoadFileNoPDFWatermarkCos(String id,HttpServletRequest request, HttpServletResponse response,String userName) {
if(StringUtils.isBlank(id)){
throw new JeroBootException("参数信息不全");
}
id = id.split("\\.")[0];
// 查询数据表数据是否存在
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;
File newFile =null;
try {
String fileName = "";
//本地下载
String filePath = ossFile.getUrl();
if(!CosBootUtil.doesObjectExist(filePath)){
response.setStatus(404);
throw new RuntimeException("文件不存在..");
}
// 文件名称
fileName = ossFile.getFileName();
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
File file = new File(filePath);
inputStream = CosBootUtil.download(filePath);
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);
}
}
if (newFile != null) {
try {
newFile.delete();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
}
/**
* 文件下载
*