认证-参数项收集-下载pdf无水印
This commit is contained in:
+81
-15
@@ -7,7 +7,6 @@ import com.jero.common.api.vo.Result;
|
|||||||
import com.jero.common.constant.enums.CutEnum;
|
import com.jero.common.constant.enums.CutEnum;
|
||||||
import com.jero.common.exception.JeroBootException;
|
import com.jero.common.exception.JeroBootException;
|
||||||
import com.jero.common.system.api.ISysBaseAPI;
|
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.RestUtil;
|
||||||
import com.jero.common.util.TokenUtils;
|
import com.jero.common.util.TokenUtils;
|
||||||
import com.jero.common.util.oConvertUtils;
|
import com.jero.common.util.oConvertUtils;
|
||||||
@@ -19,7 +18,6 @@ import io.swagger.annotations.Api;
|
|||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.shiro.SecurityUtils;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
@@ -27,12 +25,7 @@ import org.springframework.http.HttpMethod;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.http.server.ServletServerHttpRequest;
|
import org.springframework.http.server.ServletServerHttpRequest;
|
||||||
import org.springframework.util.AntPathMatcher;
|
import org.springframework.util.AntPathMatcher;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
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.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||||
import org.springframework.web.servlet.HandlerMapping;
|
import org.springframework.web.servlet.HandlerMapping;
|
||||||
@@ -41,12 +34,7 @@ import org.springframework.web.servlet.ModelAndView;
|
|||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.BufferedInputStream;
|
import java.io.*;
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@@ -382,12 +370,13 @@ public class CommonController {
|
|||||||
fileName = ossFile.getFileName();
|
fileName = ossFile.getFileName();
|
||||||
response.setContentType("application/force-download");// 设置强制下载不打开
|
response.setContentType("application/force-download");// 设置强制下载不打开
|
||||||
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
|
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);
|
File file = new File(filePath);
|
||||||
if(file.getName().endsWith(".pdf") || file.getName().endsWith(".PDF")){
|
if(file.getName().endsWith(".pdf") || file.getName().endsWith(".PDF")){
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
String currentTime = sdf.format(new Date());
|
String currentTime = sdf.format(new Date());
|
||||||
String waterContent = userName+" "+currentTime;
|
String waterContent = userName+" "+currentTime;
|
||||||
|
InputStream download = CosBootUtil.download(filePath);
|
||||||
newFile = PDFUtils.PDFWatermark(download,uploadpath,file.getName(),waterContent);
|
newFile = PDFUtils.PDFWatermark(download,uploadpath,file.getName(),waterContent);
|
||||||
inputStream = new FileInputStream(newFile.getPath());
|
inputStream = new FileInputStream(newFile.getPath());
|
||||||
}else{
|
}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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件下载
|
* 文件下载
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user