调试文件解密
This commit is contained in:
@@ -0,0 +1,87 @@
|
|||||||
|
package com.jero.modules.laws.common.util;
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
public static InputStream DecryptFile(String url, String appCode, String secretKey, 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;
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
+33
@@ -1,18 +1,28 @@
|
|||||||
package com.jero.modules.laws.home.controller;
|
package com.jero.modules.laws.home.controller;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.jero.common.api.vo.Result;
|
import com.jero.common.api.vo.Result;
|
||||||
import com.jero.common.aspect.annotation.AutoLog;
|
import com.jero.common.aspect.annotation.AutoLog;
|
||||||
|
import com.jero.common.constant.CommonConstant;
|
||||||
|
import com.jero.common.exception.JeroBootException;
|
||||||
|
import com.jero.common.util.MinioUtil;
|
||||||
|
import com.jero.modules.laws.common.util.IntekeyUtils;
|
||||||
import com.jero.modules.laws.home.dto.LawsHomeNormalSearchDTO;
|
import com.jero.modules.laws.home.dto.LawsHomeNormalSearchDTO;
|
||||||
import com.jero.modules.laws.home.service.ILawsHomeSearchService;
|
import com.jero.modules.laws.home.service.ILawsHomeSearchService;
|
||||||
import com.jero.modules.laws.home.vo.LawsHomeNormalSearchStatisticsVO;
|
import com.jero.modules.laws.home.vo.LawsHomeNormalSearchStatisticsVO;
|
||||||
import com.jero.modules.laws.home.vo.LawsHomeNormalSearchVO;
|
import com.jero.modules.laws.home.vo.LawsHomeNormalSearchVO;
|
||||||
|
import com.jero.modules.oss.entity.OSSFile;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author lqt
|
* @author lqt
|
||||||
@@ -75,4 +85,27 @@ public class LawsHomeSearchController {
|
|||||||
// todo 待开发
|
// todo 待开发
|
||||||
return Result.OK(lawsHomeSearchService.getAdvancedSearchPage(homeNormalSearchDTO,pageNo,pageSize));
|
return Result.OK(lawsHomeSearchService.getAdvancedSearchPage(homeNormalSearchDTO,pageNo,pageSize));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载附件
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@PostMapping(value = "/download")
|
||||||
|
public void view(MultipartFile file,String url, String appCode, String secretKey, HttpServletResponse response) {
|
||||||
|
try (InputStream inputStream = IntekeyUtils.DecryptFile(url, appCode, secretKey, 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user