文件下载加解密

This commit is contained in:
梁琦涛
2023-11-24 18:19:13 +08:00
parent 768320745f
commit 86e0040303
9 changed files with 440 additions and 58 deletions
@@ -137,27 +137,43 @@ public class CommonController {
}
/**
* 预览图片&下载文件
* 下载文件
*
* @param id 传入文件id
* @param request
* @param response
*/
@GetMapping(value = "/download/{id}")
public void download(@PathVariable String id,HttpServletRequest request, HttpServletResponse response) {
downloadAndView(id, response);
}
/**
* 预览图片
*
* @param id 传入文件id
* @param request
* @param response
*/
@GetMapping(value = "/view/{id}")
public void view(@PathVariable String id,HttpServletRequest request, HttpServletResponse response) {
downloadAndView(id, response);
}
private void downloadAndView(@PathVariable String id, HttpServletResponse response) {
// 查询数据表数据是否存在
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(OSSFile::getId,id);
queryWrapper.eq(OSSFile::getId, id);
OSSFile ossFile = ossFileService.getOne(queryWrapper);
if( null == ossFile){
if (null == ossFile) {
throw new JeroBootException("文件不存在..");
}
String fileUrl = ossFile.getUrl();
if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)) {
//本地下载
String filePath = uploadpath + File.separator + fileUrl;
File file = new File(filePath);
if(!file.exists()){
if (!file.exists()) {
response.setStatus(404);
throw new JeroBootException("文件不存在..");
}
@@ -173,12 +189,12 @@ public class CommonController {
outputStream.write(buf, 0, len);
}
response.flushBuffer();
}catch (Exception e){
} catch (Exception e) {
log.error(FILE_VIEW_ERROR + e.getMessage());
response.setStatus(404);
e.printStackTrace();
}
}else if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
} else if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) {
// minio 下载
// 通过MinioUtil查询时 只需要桶后面的路径
String minioUrl = MinioUtil.getMinioUrl();
@@ -193,14 +209,14 @@ public class CommonController {
) {
byte[] buf = new byte[1024];
int len;
if(inputStream==null) {
if (inputStream == null) {
return;
}
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
response.flushBuffer();
}catch (Exception e){
} catch (Exception e) {
log.error(FILE_VIEW_ERROR + e.getMessage());
response.setStatus(404);
e.printStackTrace();