update 文档拆分时,解密文件
This commit is contained in:
+2
-2
@@ -1,6 +1,6 @@
|
||||
package com.jero.modules.docking.download.controller;
|
||||
|
||||
import com.jero.modules.docking.utils.IntekeyUtils;
|
||||
import com.jero.common.util.IntekeyUtils;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
@@ -28,7 +28,7 @@ public class SearchController {
|
||||
@RequestParam("secretKey") String secretKey,
|
||||
@RequestParam(value = "scope",required = false) Integer scope,
|
||||
HttpServletResponse response) {
|
||||
try (InputStream inputStream = IntekeyUtils.DecryptFile(url, appCode, secretKey, scope, file);
|
||||
try (InputStream inputStream = IntekeyUtils.decryptFile(url, appCode, secretKey, scope, file);
|
||||
OutputStream outputStream = response.getOutputStream()
|
||||
) {
|
||||
byte[] buf = new byte[1024];
|
||||
|
||||
+7
-14
@@ -1,32 +1,23 @@
|
||||
package com.jero.modules.docking.download.service.impl;
|
||||
|
||||
import com.jero.common.api.vo.ResultCommon;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import com.jero.common.system.vo.LoginUser;
|
||||
import com.jero.modules.docking.download.config.ContentCachingWrapperFilter;
|
||||
import com.jero.modules.docking.download.service.DownloadDecryptFileService;
|
||||
import com.jero.modules.docking.utils.IntekeyUtils;
|
||||
import lombok.SneakyThrows;
|
||||
import com.jero.common.util.IntekeyUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.util.ContentCachingResponseWrapper;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
@@ -151,7 +142,7 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
|
||||
decryptFile(response, file, null);
|
||||
}else if(b1){
|
||||
// 下载文件 先解密,判断组织域,加密
|
||||
try (InputStream is = IntekeyUtils.DecryptFile(decryptUrl, decryptAppCode, decryptSecretKey, null, file)) {
|
||||
try (InputStream is = IntekeyUtils.decryptFile(decryptUrl, decryptAppCode, decryptSecretKey, null, file)) {
|
||||
MultipartFile fileDecrypt = getMultipartFile(response,is, contentType, fileName);
|
||||
// 判断fileDecrypt是不是zip压缩包,如果是压缩包的话则不加密
|
||||
boolean isZipFile = false;
|
||||
@@ -159,7 +150,9 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
|
||||
if (originalFileName != null && originalFileName.endsWith(".zip")) {
|
||||
isZipFile = true;
|
||||
}
|
||||
if (isZipFile) return;
|
||||
if (isZipFile) {
|
||||
return;
|
||||
}
|
||||
// 加密
|
||||
encryptFile(response, fileDecrypt, scope);
|
||||
} catch (Exception e) {
|
||||
@@ -197,7 +190,7 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
|
||||
}
|
||||
|
||||
private void decryptFile(HttpServletResponse response, MultipartFile file, Integer scope) {
|
||||
try (InputStream is = IntekeyUtils.DecryptFile(decryptUrl, decryptAppCode, decryptSecretKey, scope, file)) {
|
||||
try (InputStream is = IntekeyUtils.decryptFile(decryptUrl, decryptAppCode, decryptSecretKey, scope, file)) {
|
||||
writeResponse(response, is);
|
||||
} catch (Exception e) {
|
||||
log.error("Exception:",e);
|
||||
@@ -206,7 +199,7 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
|
||||
}
|
||||
|
||||
private void encryptFile(HttpServletResponse response, MultipartFile file, Integer scope) {
|
||||
try (InputStream is = IntekeyUtils.DecryptFile(encryptUrl, encryptAppCode, encryptSecretKey, scope, file)) {
|
||||
try (InputStream is = IntekeyUtils.decryptFile(encryptUrl, encryptAppCode, encryptSecretKey, scope, file)) {
|
||||
writeResponse(response, is);
|
||||
} catch (Exception e) {
|
||||
log.error("Exception:",e);
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
package com.jero.modules.docking.utils;
|
||||
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.util.DigestUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* intekey解密文件相关
|
||||
*/
|
||||
@Slf4j
|
||||
public class IntekeyUtils {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(IntekeyUtils.class);
|
||||
|
||||
/**
|
||||
* 解密文件,返回InputStream,供excel使用
|
||||
*
|
||||
*
|
||||
* @param url
|
||||
* @param multipartFile
|
||||
* @return
|
||||
*/
|
||||
//todo 这个方法需要写在laws-modules-docking 里面 可以新建一个文件夹是encrypt
|
||||
public static InputStream DecryptFile(String url, String appCode, String secretKey, Integer scope, MultipartFile multipartFile) {
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
|
||||
logger.info("解密时间:" + format.format(new Date()));
|
||||
//设置校验
|
||||
Long time = System.currentTimeMillis();
|
||||
|
||||
String sign = DigestUtils.md5DigestAsHex((secretKey + time).getBytes());
|
||||
url = url + "?appCode=" + appCode + "&time=" + time + "&sign=" + sign + (Objects.isNull(scope) ? "" : "&scope=" + scope);
|
||||
File file = transferToFile(multipartFile);
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
//设置请求头
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
MediaType type = MediaType.parseMediaType("multipart/form-data");
|
||||
headers.setContentType(type);
|
||||
|
||||
//设置请求体,注意是LinkedMultiValueMap
|
||||
FileSystemResource fileSystemResource = new FileSystemResource(file);
|
||||
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
|
||||
form.add("file", fileSystemResource);
|
||||
form.add("filename", multipartFile.getOriginalFilename());
|
||||
//用HttpEntity封装整个请求报文
|
||||
HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
|
||||
System.out.println(url);
|
||||
ResponseEntity<byte[]> entity = restTemplate.exchange(url, HttpMethod.POST, files, byte[].class);
|
||||
System.out.println(entity.getStatusCode().value() + ","+ entity.getStatusCode().getReasonPhrase() + "," + Objects.requireNonNull(entity.getBody()).length);
|
||||
byte[] body = entity.getBody();
|
||||
InputStream sbs = new ByteArrayInputStream(body);
|
||||
return sbs;
|
||||
}
|
||||
|
||||
private static File transferToFile(MultipartFile multipartFile) {
|
||||
// 选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
|
||||
File file = null;
|
||||
try {
|
||||
String originalFilename = multipartFile.getOriginalFilename();
|
||||
// 如果文件名中有特殊字符,则需要使用URLDecoder解析真正的文件名
|
||||
if (StrUtil.isNotBlank(originalFilename) && originalFilename.contains("%")) {
|
||||
originalFilename = java.net.URLDecoder.decode(originalFilename, "UTF-8");
|
||||
}
|
||||
log.info("originalFilename:"+originalFilename);
|
||||
int index = originalFilename.lastIndexOf(".");
|
||||
//String[] filename = originalFilename.split("\\.");
|
||||
if (index != -1){
|
||||
file = File.createTempFile(originalFilename.substring(0, index), originalFilename.substring(index + 1));
|
||||
}else {
|
||||
file = File.createTempFile(originalFilename, null);
|
||||
}
|
||||
file.delete();
|
||||
multipartFile.transferTo(file);
|
||||
file.deleteOnExit();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String args[]) {
|
||||
String originalFilename = "C:\\Users\\LQT\\Desktop\\参考代码 - 副本.txt";
|
||||
int index = originalFilename.lastIndexOf(".");
|
||||
//String[] filename = originalFilename.split("\\.");
|
||||
System.out.println(originalFilename.substring(0, index) + "||||||||||||" + originalFilename.substring(index + 1));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user