一般检索修改
This commit is contained in:
+47
@@ -0,0 +1,47 @@
|
||||
package com.jero.modules.docking.encrypt.controller;
|
||||
|
||||
import com.jero.modules.docking.utils.IntekeyUtils;
|
||||
import io.swagger.annotations.Api;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* @author lqt
|
||||
* @version 1.0
|
||||
* @date 2023/11/7 15:22
|
||||
*/
|
||||
@Slf4j
|
||||
@Api(tags = "加解密附件")
|
||||
@RequestMapping("/encrypt")
|
||||
@RestController
|
||||
public class EncrygtController {
|
||||
|
||||
/**
|
||||
* 下载附件
|
||||
*
|
||||
*/
|
||||
@PostMapping(value = "/download")
|
||||
public void view(MultipartFile file,String url, String appCode, String secretKey,String scope, HttpServletResponse response) {
|
||||
try (InputStream inputStream = IntekeyUtils.DecryptFile(url, appCode, secretKey, scope, file);
|
||||
OutputStream outputStream = response.getOutputStream()
|
||||
) {
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = inputStream.read(buf)) > 0) {
|
||||
outputStream.write(buf, 0, len);
|
||||
}
|
||||
response.flushBuffer();
|
||||
}catch (Exception e){
|
||||
log.error(e.getMessage());
|
||||
response.setStatus(404);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.jero.modules.docking.utils;
|
||||
|
||||
|
||||
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.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解密文件相关
|
||||
*/
|
||||
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, String 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 + "&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();
|
||||
int index = originalFilename.lastIndexOf(".");
|
||||
//String[] filename = originalFilename.split("\\.");
|
||||
file = File.createTempFile(originalFilename.substring(0, index), originalFilename.substring(index + 1));
|
||||
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