From e8b34d51447fe08394587c7977cd2b6d8db274ee Mon Sep 17 00:00:00 2001 From: mzc5649 <13027087095@163.com> Date: Mon, 29 Mar 2021 17:28:27 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91CommonContr?= =?UTF-8?q?oller=E4=BF=AE=E6=94=B9=E4=B8=8A=E4=BC=A0=E4=B8=8E=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=96=87=E4=BB=B6=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/controller/CommonController.java | 41 ++++++------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java index 1ca33e48..ed7c5bf9 100644 --- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java @@ -109,58 +109,43 @@ public class CommonController { /** * 预览图片&下载文件 - * 请求地址:http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg} * + * @param id 传入文件id * @param request * @param response */ - @GetMapping(value = "/download/**") - public void view(HttpServletRequest request, HttpServletResponse response) { - // ISO-8859-1 ==> UTF-8 进行编码转换 - String imgPath = extractPathFromPattern(request); - if(oConvertUtils.isEmpty(imgPath) || imgPath == "null"){ - return; + @GetMapping(value = "/download/{id}") + public void view(@PathVariable String id,HttpServletRequest request, HttpServletResponse response) { + // 查询数据表数据是否存在 + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(OSSFile::getId,id); + OSSFile ossFile = ossFileService.getOne(queryWrapper); + if( null == ossFile){ + throw new RuntimeException("文件不存在.."); } - // 其余处理略 + String fileUrl = ossFile.getUrl(); InputStream inputStream = null; OutputStream outputStream = null; try { - imgPath = imgPath.replace("..", ""); - if (imgPath.endsWith(",")) { - imgPath = imgPath.substring(0, imgPath.length() - 1); - } String fileName = ""; - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ //本地下载 - String filePath = uploadpath + File.separator + imgPath; + String filePath = uploadpath + File.separator + fileName; File file = new File(filePath); if(!file.exists()){ response.setStatus(404); throw new RuntimeException("文件不存在.."); } - // 查询数据表数据是否存在 - queryWrapper.eq(OSSFile::getUrl,imgPath); - OSSFile ossFile = ossFileService.getOne(queryWrapper); - if( null == ossFile){ - throw new RuntimeException("文件不存在.."); - } // 文件名称 fileName = file.getName(); inputStream = new BufferedInputStream(new FileInputStream(filePath)); }else if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){ // minio 下载 - queryWrapper.eq(OSSFile::getUrl,imgPath.replaceFirst("/","//")); - OSSFile ossFile = ossFileService.getOne(queryWrapper); - // 查询该文件数据是否存在 - if( null == ossFile){ - throw new RuntimeException("文件不存在"); - } - // 通过MinioUtil查询时 只需要 桶后面的路径 + // 通过MinioUtil查询时 只需要桶后面的路径 String minioUrl = MinioUtil.getMinioUrl(); // Linux/unix 系统下文件路径分隔符为"/" 获取minio与存储桶的路径 minioUrl = minioUrl + MinioUtil.getBucketName() + "/"; - String url = ossFile.getUrl().replace(minioUrl, ""); + String url = fileUrl.replace(minioUrl, ""); // 文件名称 fileName = ossFile.getFileName(); inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);