update 文档拆分时,解密文件
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
package com.jero.common.util;
|
||||
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.DigestUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
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
|
||||
@Component
|
||||
public class IntekeyUtils {
|
||||
public static boolean ENABLE;
|
||||
public static String DECRYPT_URL;
|
||||
public static String DECRYPT_APP_CODE;
|
||||
public static String DECRYPT_SECRET_KEY;
|
||||
|
||||
@Value("${download.enable}")
|
||||
public void setEnable(boolean enable) {
|
||||
ENABLE = enable;
|
||||
}
|
||||
@Value("${download.decrypt.url}")
|
||||
public void setDecryptUrl(String decryptUrl) {
|
||||
DECRYPT_URL = decryptUrl;
|
||||
}
|
||||
@Value("${download.decrypt.app_code}")
|
||||
public void setDecryptAppCode(String decryptAppCode) {
|
||||
DECRYPT_APP_CODE = decryptAppCode;
|
||||
}
|
||||
@Value("${download.decrypt.secret_key}")
|
||||
public void setDecryptSecretKey(String decryptSecretKey) {
|
||||
DECRYPT_SECRET_KEY = decryptSecretKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密文件,返回InputStream,供excel使用
|
||||
*
|
||||
*
|
||||
* @param url
|
||||
* @param multipartFile
|
||||
* @return
|
||||
*/
|
||||
public static InputStream decryptFile(String url, String appCode, String secretKey, Integer scope, MultipartFile multipartFile) {
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
|
||||
log.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 InputStream getInputStreamByDecryptFile(MultipartFile file) {
|
||||
if(ENABLE){
|
||||
return decryptFile(DECRYPT_URL, DECRYPT_APP_CODE, DECRYPT_SECRET_KEY, null, file);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
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