文件下载加解密

This commit is contained in:
梁琦涛
2023-11-27 18:22:29 +08:00
parent 9abc636637
commit aaf1a5c810
4 changed files with 66 additions and 20 deletions
@@ -0,0 +1,56 @@
package com.jero.modules.docking.download.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jero.common.api.vo.Result;
import com.jero.common.aspect.annotation.AutoLog;
import com.jero.common.constant.CommonConstant;
import com.jero.common.exception.JeroBootException;
import com.jero.common.util.CommonUtils;
import com.jero.common.util.MinioUtil;
import com.jero.common.util.oConvertUtils;
import com.jero.modules.docking.utils.IntekeyUtils;
import com.jero.modules.laws.home.dto.LawsHomeNormalSearchDTO;
import com.jero.modules.laws.home.vo.LawsHomeNormalSearchVO;
import com.jero.modules.oss.entity.OSSFile;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
* @author lqt
* @version 1.0
* @date 2023/11/27 17:41
*/
@RestController
@RequestMapping("/home/search")
public class SearchController {
@PostMapping(value = "/download")
private void downloadAndView(@RequestParam("file") MultipartFile file,
@RequestParam("url") String url,
@RequestParam("appCode") String appCode,
@RequestParam("secretKey") String secretKey,
@RequestParam("scope") Integer 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) {
response.setStatus(404);
e.printStackTrace();
}
}
}
@@ -107,13 +107,13 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
}
} catch (Exception e) {
e.printStackTrace();
throw new JeroBootException(ResultCommon.ERROR);
response.setStatus(404);
}
String contentType = response.getContentType();
String fileName = getFileName(response);
MultipartFile file = getMultipartFile(responseBody, contentType, fileName);
MultipartFile file = getMultipartFile(response,responseBody, contentType, fileName);
// 访问路径
String str = request.getRequestURI();
@@ -127,12 +127,12 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
}else if(b1){
// 下载文件 先解密,判断组织域,加密
try (InputStream is = IntekeyUtils.DecryptFile(decryptUrl, decryptAppCode, decryptSecretKey, null, file)) {
MultipartFile fileDecrypt = getMultipartFile(is, contentType, fileName);
MultipartFile fileDecrypt = getMultipartFile(response,is, contentType, fileName);
int scope = getScope();
// 加密
encryptFile(response, fileDecrypt, scope);
} catch (Exception e) {
throw new JeroBootException(ResultCommon.ERROR);
response.setStatus(404);
}
}else{
@@ -166,7 +166,7 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
try (InputStream is = IntekeyUtils.DecryptFile(decryptUrl, decryptAppCode, decryptSecretKey, scope, file)) {
writeResponse(response, is);
} catch (Exception e) {
throw new JeroBootException(ResultCommon.ERROR);
response.setStatus(404);
}
}
@@ -174,18 +174,18 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
try (InputStream is = IntekeyUtils.DecryptFile(encryptUrl, encryptAppCode, encryptSecretKey, scope, file)) {
writeResponse(response, is);
} catch (Exception e) {
throw new JeroBootException(ResultCommon.ERROR);
response.setStatus(404);
}
}
@NotNull
private MultipartFile getMultipartFile(InputStream responseBody,String contentType,String fileName) {
MultipartFile file;
private MultipartFile getMultipartFile(HttpServletResponse response,InputStream responseBody,String contentType,String fileName) {
MultipartFile file = null;
try {
file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(),fileName,contentType, responseBody);
} catch (IOException e) {
e.printStackTrace();
throw new JeroBootException(ResultCommon.ERROR);
response.setStatus(404);
}
return file;
}