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 new file mode 100644 index 00000000..34a473c3 --- /dev/null +++ b/virtual-patient-manage/src/main/java/com/supervision/manage/controller/file/FileManageController.java @@ -0,0 +1,34 @@ +package com.supervision.manage.controller.file; + +import com.supervision.manage.service.FileManageService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; + +@Api(tags = "文件管理") +@RestController +@RequestMapping("/fileManage") +@RequiredArgsConstructor +public class FileManageController { + + private final FileManageService fileManageService; + @ApiOperation("上传文件") + @PostMapping("/uploadFile") + public String uploadFile(@RequestParam("file") MultipartFile multipartFile) throws Exception { + + return fileManageService.uploadFile(multipartFile,multipartFile.getContentType()); + + } + + @ApiOperation("下载文件") + @GetMapping("/downloadFile") + public void downloadFile(@RequestParam String fileId, HttpServletResponse response) throws Exception { + + fileManageService.downloadFile(fileId, response); + + } +} 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 new file mode 100644 index 00000000..12b926bb --- /dev/null +++ b/virtual-patient-manage/src/main/java/com/supervision/manage/service/FileManageService.java @@ -0,0 +1,11 @@ +package com.supervision.manage.service; + +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; + +public interface FileManageService { + String uploadFile(MultipartFile multipartFile, String contentType) throws Exception; + + void downloadFile(String fileId, HttpServletResponse response) 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 new file mode 100644 index 00000000..15dd93a8 --- /dev/null +++ b/virtual-patient-manage/src/main/java/com/supervision/manage/service/impl/FileManageServiceImpl.java @@ -0,0 +1,59 @@ +package com.supervision.manage.service.impl; + +import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.StrUtil; +import com.supervision.manage.service.FileManageService; +import com.supervision.model.FileResource; +import com.supervision.service.FileResourceService; +import com.supervision.util.MinioUtil; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.InputStream; + +@Service +@RequiredArgsConstructor +public class FileManageServiceImpl implements FileManageService { + + private final FileResourceService fileResourceService; + @Override + public String uploadFile(MultipartFile multipartFile, String contentType) throws Exception { + // 把文件上传到minio + String minoId = MinioUtil.uploadFile(multipartFile.getInputStream(), contentType); + + //设置文件信息 + FileResource fileResource = new FileResource(); + fileResource.setFileName(multipartFile.getOriginalFilename()); + fileResource.setFileSize(multipartFile.getSize()); + fileResource.setMinioId(minoId); + fileResource.setFileType(contentType); + fileResourceService.save(fileResource); + + return fileResource.getId(); + } + + @Override + public void downloadFile(String fileId, HttpServletResponse response) throws Exception { + Assert.notEmpty(fileId, "文件id不能为空"); + + FileResource fileResource = fileResourceService.getById(fileId); + Assert.notNull(fileResource, "文件不存在"); + + try (InputStream inputStream = MinioUtil.download(fileResource.getMinioId()); + ServletOutputStream outputStream = response.getOutputStream()){ + if (StrUtil.isNotEmpty(fileResource.getFileType())){ + response.setContentType(fileResource.getFileType()); + } + + byte[] bytes = new byte[1024]; + int len; + while ((len = inputStream.read(bytes))!= -1){ + outputStream.write(bytes, 0, len); + } + outputStream.flush(); + } + } +}