三个测试接口

This commit is contained in:
yuezhihang
2022-03-08 15:48:44 +08:00
parent 262ed6f777
commit 81ef7787e8
@@ -128,6 +128,58 @@ public class AttFileEOController {
}
}
@ApiOperation(value="|File|上传文件测试加密解密用")
@PostMapping(value="/uploadNew",consumes="multipart/*",headers="content-type=multipart/form-data" )
@CrossOrigin(origins = "*", maxAge = 3600)
// @RequiresPermissions("att:attFile:uploadFile")
public ResponseMessage<AttFileVo> uploadFileNew(@RequestParam("file") @ApiParam(value="上传文件",required=true) MultipartFile file) throws Exception{
if(StringUtils.isNotEmpty(uploadFileWhiteLists)){
String[] fileLists = uploadFileWhiteLists.split(",");
List<String> arrList = Arrays.asList(fileLists);
String getFileName = file.getOriginalFilename();
Pattern pReg = Pattern.compile("\\/|\\/|\\||:|\\?|\\%|\\*|\"|<|>|\\p{Cntrl}");
// getFileName = getFileName.replaceAll(, "_");
Matcher matcher = pReg.matcher(getFileName);
if (matcher.find()) {
return Result.error("文件上传失败,该文件名可能导致文件类型改变,请修改后重试");
}
//截取文件后缀
int pos = getFileName.lastIndexOf(".");
String str = getFileName.substring(pos+1).toLowerCase();
if (arrList.contains(str)) {
AttFileVo fileInfo = attFileEOService.saveFileInfo(file);
if (fileInfo != null && fileInfo.getId() != null) {
String oriFileName = fileInfo.getOldFileName();
if (StringUtils.isNotEmpty(oriFileName)) {
String standNumber = "";
oriFileName = oriFileName.replaceAll("."+fileInfo.getFileSuffix(),"");
Pattern ptest = Pattern.compile("[A-Z]{1,}/{0,1}[A-Z]{1,}\\s{0,1}[0-9]\\d*\\.?\\d*");
Pattern ptest2 = Pattern.compile("[A-Z]{1,}/{0,1}[A-Z]{1,}\\s{0,1}[0-9]\\d*\\.?\\d*-[0-9]{1,4}");
Matcher matcher1 = ptest.matcher(oriFileName);
Matcher matcher2 = ptest2.matcher(oriFileName);
if (matcher2.find()) {
standNumber = matcher2.group();
} else if (matcher1.find()) {
standNumber = matcher1.group();
}
fileInfo.setStandNum(standNumber);
String standName = oriFileName.replace(standNumber,"");
fileInfo.setStandName(standName);
}
// 上传转换文件
fileToConvert();
return Result.success("true", "上传成功", fileInfo);
} else {
return Result.error("文件上传失败");
}
} else {
return Result.error("文件上传失败,不允许上传该类型文件");
}
} else {
return Result.error("文件上传失败,不允许上传该类型文件");
}
}
@Async
void fileToConvert(){
@@ -271,6 +323,31 @@ public class AttFileEOController {
}
}
@ApiOperation(value = "|File|下载文件")
@GetMapping("/downloadFileForSarNew")
// @RequiresPermissions("sys:file:downloadFileForSar")
public void downloadFileForSarNew(String fileId, HttpServletResponse response, HttpServletRequest request) throws Exception {
AttFileEO attFileEO = attFileEOService.getFileInfo(fileId);
InputStream is = null;
OutputStream os = null;
response.reset();
try {
String fileOldName = fileNameEncoding(attFileEO.getOldFileName(),request);
response.setHeader("Content-Disposition", "attachment;filename=\""+fileOldName+"\"");
response.setContentType("application/octet-stream");
File file = new File(filePath+attFileEO.getFilePath()+attFileEO.getFileName());
is = new FileInputStream(file);
os = response.getOutputStream();
IOUtils.copy(is, os);
os.flush();
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
}
@ApiOperation(value = "|File|下载文件加水印")
@GetMapping("/downloadFileForSarWaterMark")
// @RequiresPermissions("sys:file:downloadFileForSar")
@@ -331,6 +408,66 @@ public class AttFileEOController {
}
}
@ApiOperation(value = "|File|下载文件加水印")
@GetMapping("/downloadFileForSarWaterMarkNew")
// @RequiresPermissions("sys:file:downloadFileForSar")
public void downloadFileForSarWaterMarkNew(String userId,String fileId, HttpServletResponse response, HttpServletRequest request) throws Exception {
AttFileEO attFileEO = attFileEOService.getFileInfo(fileId);
InputStream is = null;
OutputStream os = null;
response.reset();
try {
String fileOldName = fileNameEncoding(attFileEO.getOldFileName(),request);
//生成一份水印文件
String oldFilePath = filePath + attFileEO.getFilePath()+attFileEO.getFileName();
String newFilePath = filePath + attFileEO.getFilePath()+"waterPath/";
File dir = new File(newFilePath);
if (!dir.exists()) {
dir.mkdirs();
}
String waterFilePath = newFilePath + attFileEO.getOldFileName();
// 添加水印
String userMsg = "";
TsUser tsUser = userDao.selectById(userId);
if (tsUser != null) {
userMsg = tsUser.getUname();
// if (StringUtils.isNotEmpty(tsUser.getWorkNum())) {
// userMsg += tsUser.getWorkNum() + ",";
// }
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");//设置日期格式
String date = df.format(new Date());
String waterContent = userMsg;
//2020年9月5日 去掉水印处理
//2021年7月25日要求下载生成水印
WaterMarkUtil.waterMark(oldFilePath,waterFilePath,waterContent);
response.setHeader("Content-Disposition", "attachment;filename=\""+fileOldName+"\"");
response.setContentType("application/octet-stream");
String downloadFilePath="";
String fileName=attFileEO.getFileName();
String suffix=fileName.substring(fileName.lastIndexOf(".")+1);
//只能在pdf中添加水印,
//非pdf文件直接下载原文件
if (suffix.equals("pdf")||suffix.equals("PDF")){
downloadFilePath=waterFilePath;
}else {
downloadFilePath=oldFilePath;
}
File file = new File(downloadFilePath);
is = new FileInputStream(file);
os = response.getOutputStream();
IOUtils.copy(is, os);
os.flush();
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
}
/**
* @Author super_liu
* @Description 根据不同浏览器定义下载文件编码