From ed7673de24d853187f9b5c1d847f0d063c9ecc15 Mon Sep 17 00:00:00 2001 From: xueqingkun Date: Tue, 4 Jun 2024 15:45:30 +0800 Subject: [PATCH] =?UTF-8?q?1:=20manage:=20=E6=B7=BB=E5=8A=A0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=96=87=E4=BB=B6base64=E7=9A=84=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/file/FileManageController.java | 9 ++++++++ .../manage/service/FileManageService.java | 2 ++ .../service/impl/FileManageServiceImpl.java | 23 +++++++++++++++---- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/virtual-patient-manage/src/main/java/com/supervision/manage/controller/file/FileManageController.java b/virtual-patient-manage/src/main/java/com/supervision/manage/controller/file/FileManageController.java index 37d958a9..db6c442c 100644 --- a/virtual-patient-manage/src/main/java/com/supervision/manage/controller/file/FileManageController.java +++ b/virtual-patient-manage/src/main/java/com/supervision/manage/controller/file/FileManageController.java @@ -34,6 +34,15 @@ public class FileManageController { } + + @Operation(summary = "获取文件base64的编码") + @GetMapping("/loadFileBase64") + public String loadFileBase64(@RequestParam String fileId) throws Exception { + + return fileManageService.loadFileBase64(fileId); + + } + @Operation(summary = "删除文件") @DeleteMapping("/deleteFile") public boolean deleteFile(@RequestParam String fileId) throws Exception { diff --git a/virtual-patient-manage/src/main/java/com/supervision/manage/service/FileManageService.java b/virtual-patient-manage/src/main/java/com/supervision/manage/service/FileManageService.java index 9e37b0be..e8f5a720 100644 --- a/virtual-patient-manage/src/main/java/com/supervision/manage/service/FileManageService.java +++ b/virtual-patient-manage/src/main/java/com/supervision/manage/service/FileManageService.java @@ -19,4 +19,6 @@ public interface FileManageService { File downloadFile(String fileId) throws Exception; boolean deleteFile(String fileId) throws Exception; + + String loadFileBase64(String fileId) throws Exception; } diff --git a/virtual-patient-manage/src/main/java/com/supervision/manage/service/impl/FileManageServiceImpl.java b/virtual-patient-manage/src/main/java/com/supervision/manage/service/impl/FileManageServiceImpl.java index f800ba5d..8fb08cdb 100644 --- a/virtual-patient-manage/src/main/java/com/supervision/manage/service/impl/FileManageServiceImpl.java +++ b/virtual-patient-manage/src/main/java/com/supervision/manage/service/impl/FileManageServiceImpl.java @@ -1,9 +1,11 @@ package com.supervision.manage.service.impl; +import cn.hutool.core.codec.Base64; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.StrUtil; +import com.supervision.exception.BusinessException; import com.supervision.manage.service.FileManageService; import com.supervision.model.FileResource; import com.supervision.service.FileResourceService; @@ -15,10 +17,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; +import java.io.*; import java.net.URLEncoder; import java.util.List; import java.util.Objects; @@ -95,6 +94,9 @@ public class FileManageServiceImpl implements FileManageService { outputStream.write(bytes, 0, len); } outputStream.flush(); + }catch (Exception e){ + log.error("文件下载失败",e); + throw new BusinessException("下载文件失败",e); } } @@ -123,6 +125,12 @@ public class FileManageServiceImpl implements FileManageService { fileOutputStream.write(bytes, 0, len); } fileOutputStream.flush(); + }catch (Exception e){ + if (tempFile.exists()){ + tempFile.delete(); + } + log.error("文件下载失败",e); + throw new BusinessException("文件下载失败",e); } return tempFile; } @@ -142,4 +150,11 @@ public class FileManageServiceImpl implements FileManageService { } return fileResourceService.removeById(fileId); } + + @Override + public String loadFileBase64(String fileId) throws Exception { + File file = downloadFile(fileId); + + return Base64.encode(new FileInputStream(file)); + } }