From dde18d75b0bd4f07833e8f65df4e93311a9fb762 Mon Sep 17 00:00:00 2001 From: linrf Date: Wed, 19 Jul 2023 17:12:16 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E9=87=87=E9=9B=86=E8=B0=83=E5=BA=A6?= =?UTF-8?q?=E5=99=A8-=E5=90=8E=E7=AB=AFHTTP=20API=E3=80=91=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E9=87=87=E9=9B=86=E5=99=A8=E7=AE=A1=E7=90=86=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9=E5=8C=85API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/FileController.java | 31 +++++++++++++++ .../server/service/IFileUploadService.java | 3 +- .../service/impl/FileUploadServiceImpl.java | 35 ++++++++++------- .../api/scheduling.management/FileApi.java | 39 +++++++++++++++++++ .../DownloadFileDTO.java | 28 +++++++++++++ 5 files changed, 119 insertions(+), 17 deletions(-) create mode 100644 collector-scheduling-management/src/main/java/com/docus/server/controller/FileController.java create mode 100644 docus-client-interface/src/main/java/com/docus/server/api/scheduling.management/FileApi.java create mode 100644 docus-client-interface/src/main/java/com/docus/server/dto/scheduling.management/schcollectorversionfile/DownloadFileDTO.java diff --git a/collector-scheduling-management/src/main/java/com/docus/server/controller/FileController.java b/collector-scheduling-management/src/main/java/com/docus/server/controller/FileController.java new file mode 100644 index 0000000..dc42eab --- /dev/null +++ b/collector-scheduling-management/src/main/java/com/docus/server/controller/FileController.java @@ -0,0 +1,31 @@ +package com.docus.server.controller; + +import com.docus.server.api.scheduling.management.FileApi; +import com.docus.server.service.IFileUploadService; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; + +/** + * 文件上传下载 API + * + * @author AutoGenerator + * @since 2023-07-15 + */ +@RestController +public class FileController implements FileApi { + @Resource + private IFileUploadService iFileUploadService; + + @Override + public void downloadFile(String filePath, HttpServletResponse response) throws Exception { + iFileUploadService.downloadFile(filePath, response); + } + + @Override + public void uploadFile(MultipartFile[] multipartFiles, String pathKey) throws Exception { + iFileUploadService.uploadFile(multipartFiles, pathKey); + } +} diff --git a/collector-scheduling-management/src/main/java/com/docus/server/service/IFileUploadService.java b/collector-scheduling-management/src/main/java/com/docus/server/service/IFileUploadService.java index 8bd193b..00e46ec 100644 --- a/collector-scheduling-management/src/main/java/com/docus/server/service/IFileUploadService.java +++ b/collector-scheduling-management/src/main/java/com/docus/server/service/IFileUploadService.java @@ -10,6 +10,5 @@ public interface IFileUploadService { List uploadFile(MultipartFile[] multipartFiles, String pathKey) throws Exception; - void downloadFile(HttpServletResponse response); - + void downloadFile(String filePath, HttpServletResponse response); } diff --git a/collector-scheduling-management/src/main/java/com/docus/server/service/impl/FileUploadServiceImpl.java b/collector-scheduling-management/src/main/java/com/docus/server/service/impl/FileUploadServiceImpl.java index b52cd96..647a2bb 100644 --- a/collector-scheduling-management/src/main/java/com/docus/server/service/impl/FileUploadServiceImpl.java +++ b/collector-scheduling-management/src/main/java/com/docus/server/service/impl/FileUploadServiceImpl.java @@ -6,6 +6,7 @@ import com.docus.infrastructure.web.exception.ApiException; import com.docus.infrastructure.web.exception.ExceptionCode; import com.docus.server.service.IFileUploadService; import com.docus.server.vo.scheduling.management.schcollectorversionfile.UploadFileVO; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -24,6 +25,7 @@ import java.util.List; import java.util.UUID; @Service +@Slf4j public class FileUploadServiceImpl implements IFileUploadService { @Value("${file.uploadFolder:D://docus/}") @@ -44,21 +46,10 @@ public class FileUploadServiceImpl implements IFileUploadService { for (MultipartFile multipartFile : multipartFiles) { - if (multipartFile.isEmpty()) { - throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "上传失败,请选择文件!"); - } - - String originalFilename = multipartFile.getOriginalFilename(); - if (StrUtil.isBlank(originalFilename)) { - throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "未找到原文件文件名!"); - } - if (!originalFilename.contains(".")) { - throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "原文件名未识别到后缀!"); - } + valid(multipartFile); uploadFileVOList.add(actionUploadFile(multipartFile, pathKey)); } - return uploadFileVOList; } @@ -90,10 +81,11 @@ public class FileUploadServiceImpl implements IFileUploadService { } @Override - public void downloadFile(HttpServletResponse response) { + public void downloadFile(String path, HttpServletResponse response) { + try { //获取要下载的文件 - File file = new File("file_path"); + File file = new File(saveFilePath + "/" + path); //设置响应的内容类型为二进制流,即文件类型 response.setContentType("application/octet-stream"); @@ -124,9 +116,22 @@ public class FileUploadServiceImpl implements IFileUploadService { buffInputStream.close(); inputStream.close(); } catch (Exception e) { - e.printStackTrace(); + log.error(e.getMessage(), e); } } + private void valid(MultipartFile multipartFile) { + if (multipartFile.isEmpty()) { + throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "上传失败,请选择文件!"); + } + + String originalFilename = multipartFile.getOriginalFilename(); + if (StrUtil.isBlank(originalFilename)) { + throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "未找到原文件文件名!"); + } + if (!originalFilename.contains(".")) { + throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "原文件名未识别到后缀!"); + } + } } diff --git a/docus-client-interface/src/main/java/com/docus/server/api/scheduling.management/FileApi.java b/docus-client-interface/src/main/java/com/docus/server/api/scheduling.management/FileApi.java new file mode 100644 index 0000000..e4b53b6 --- /dev/null +++ b/docus-client-interface/src/main/java/com/docus/server/api/scheduling.management/FileApi.java @@ -0,0 +1,39 @@ +package com.docus.server.api.scheduling.management; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; + + +/** + * 文件上传下载 API + * + * @author AutoGenerator + * @since 2023-07-15 + */ +@Api(value = "通用文件上传下载接口", tags = "通用文件上传下载接口") +@FeignClient(value = "collector-scheduling-management", contextId = "collector-scheduling-management.FileApi") +@RequestMapping("/sch/file") +public interface FileApi { + + @ApiOperation("文件下载") + @GetMapping("/download") + void downloadFile(@RequestParam(value = "filePath") String filePath, HttpServletResponse response) throws Exception; + + @ApiOperation("文件上传") + @PostMapping("/upload") + @ApiImplicitParams({ + @ApiImplicitParam(name = "files", value = "文件", required = true, dataTypeClass = MultipartFile.class) + }) + void uploadFile(MultipartFile[] multipartFiles, String pathKey) throws Exception; + +} diff --git a/docus-client-interface/src/main/java/com/docus/server/dto/scheduling.management/schcollectorversionfile/DownloadFileDTO.java b/docus-client-interface/src/main/java/com/docus/server/dto/scheduling.management/schcollectorversionfile/DownloadFileDTO.java new file mode 100644 index 0000000..c8d8d17 --- /dev/null +++ b/docus-client-interface/src/main/java/com/docus/server/dto/scheduling.management/schcollectorversionfile/DownloadFileDTO.java @@ -0,0 +1,28 @@ +package com.docus.server.dto.scheduling.management.schcollectorversionfile; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +/** + * 采集器版本列表更新包管理 AddDTO + * + * @author AutoGenerator + * @since 2023-07-15 + */ +@Data +@ApiModel(value = "DownloadFileDTO", description = "DownloadFileDTO") +public class DownloadFileDTO implements Serializable { + + @NotNull(message = "采集器文件路径不能为空") + @ApiModelProperty(value = "采集器文件路径") + private String filePath; + +// @NotNull(message = "采集器文件保存路径不能为空") +// @ApiModelProperty(value = "采集器文件保存路径") +// private String savePath; + +}