feat: 通用文件上传解密方法
This commit is contained in:
@@ -261,6 +261,10 @@
|
|||||||
<groupId>p6spy</groupId>
|
<groupId>p6spy</groupId>
|
||||||
<artifactId>p6spy</artifactId>
|
<artifactId>p6spy</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -6,6 +6,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.core.io.FileSystemResource;
|
import org.springframework.core.io.FileSystemResource;
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
|
import org.springframework.mock.web.MockMultipartFile;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.DigestUtils;
|
import org.springframework.util.DigestUtils;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
@@ -33,23 +34,46 @@ public class IntekeyUtils {
|
|||||||
public void setEnable(boolean enable) {
|
public void setEnable(boolean enable) {
|
||||||
ENABLE = enable;
|
ENABLE = enable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Value("${download.decrypt.url}")
|
@Value("${download.decrypt.url}")
|
||||||
public void setDecryptUrl(String decryptUrl) {
|
public void setDecryptUrl(String decryptUrl) {
|
||||||
DECRYPT_URL = decryptUrl;
|
DECRYPT_URL = decryptUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Value("${download.decrypt.app_code}")
|
@Value("${download.decrypt.app_code}")
|
||||||
public void setDecryptAppCode(String decryptAppCode) {
|
public void setDecryptAppCode(String decryptAppCode) {
|
||||||
DECRYPT_APP_CODE = decryptAppCode;
|
DECRYPT_APP_CODE = decryptAppCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Value("${download.decrypt.secret_key}")
|
@Value("${download.decrypt.secret_key}")
|
||||||
public void setDecryptSecretKey(String decryptSecretKey) {
|
public void setDecryptSecretKey(String decryptSecretKey) {
|
||||||
DECRYPT_SECRET_KEY = decryptSecretKey;
|
DECRYPT_SECRET_KEY = decryptSecretKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static MultipartFile autoDecryptFile(MultipartFile file) {
|
||||||
|
if (!ENABLE || file == null) {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将文件解密
|
||||||
|
InputStream inputStream;
|
||||||
|
try {
|
||||||
|
inputStream = IntekeyUtils.decryptFile(DECRYPT_URL, DECRYPT_APP_CODE, DECRYPT_SECRET_KEY, null, file);
|
||||||
|
return new MockMultipartFile(
|
||||||
|
file.getName(),
|
||||||
|
file.getOriginalFilename(),
|
||||||
|
file.getContentType(),
|
||||||
|
inputStream);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("文件解密失败,返回原文件");
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解密文件,返回InputStream,供excel使用
|
* 解密文件,返回InputStream,供excel使用
|
||||||
*
|
*
|
||||||
*
|
|
||||||
* @param url
|
* @param url
|
||||||
* @param multipartFile
|
* @param multipartFile
|
||||||
* @return
|
* @return
|
||||||
@@ -78,7 +102,7 @@ public class IntekeyUtils {
|
|||||||
HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
|
HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
|
||||||
System.out.println(url);
|
System.out.println(url);
|
||||||
ResponseEntity<byte[]> entity = restTemplate.exchange(url, HttpMethod.POST, files, byte[].class);
|
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);
|
System.out.println(entity.getStatusCode().value() + "," + entity.getStatusCode().getReasonPhrase() + "," + Objects.requireNonNull(entity.getBody()).length);
|
||||||
byte[] body = entity.getBody();
|
byte[] body = entity.getBody();
|
||||||
InputStream sbs = new ByteArrayInputStream(body);
|
InputStream sbs = new ByteArrayInputStream(body);
|
||||||
return sbs;
|
return sbs;
|
||||||
@@ -93,12 +117,12 @@ public class IntekeyUtils {
|
|||||||
if (StrUtil.isNotBlank(originalFilename) && originalFilename.contains("%")) {
|
if (StrUtil.isNotBlank(originalFilename) && originalFilename.contains("%")) {
|
||||||
originalFilename = java.net.URLDecoder.decode(originalFilename, "UTF-8");
|
originalFilename = java.net.URLDecoder.decode(originalFilename, "UTF-8");
|
||||||
}
|
}
|
||||||
log.info("originalFilename:"+originalFilename);
|
log.info("originalFilename:" + originalFilename);
|
||||||
int index = originalFilename.lastIndexOf(".");
|
int index = originalFilename.lastIndexOf(".");
|
||||||
//String[] filename = originalFilename.split("\\.");
|
//String[] filename = originalFilename.split("\\.");
|
||||||
if (index != -1){
|
if (index != -1) {
|
||||||
file = File.createTempFile(originalFilename.substring(0, index), originalFilename.substring(index + 1));
|
file = File.createTempFile(originalFilename.substring(0, index), originalFilename.substring(index + 1));
|
||||||
}else {
|
} else {
|
||||||
file = File.createTempFile(originalFilename, null);
|
file = File.createTempFile(originalFilename, null);
|
||||||
}
|
}
|
||||||
file.delete();
|
file.delete();
|
||||||
@@ -111,7 +135,7 @@ public class IntekeyUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static InputStream getInputStreamByDecryptFile(MultipartFile file) {
|
public static InputStream getInputStreamByDecryptFile(MultipartFile file) {
|
||||||
if(ENABLE){
|
if (ENABLE) {
|
||||||
return decryptFile(DECRYPT_URL, DECRYPT_APP_CODE, DECRYPT_SECRET_KEY, null, file);
|
return decryptFile(DECRYPT_URL, DECRYPT_APP_CODE, DECRYPT_SECRET_KEY, null, file);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user