feat(file): 优化文件上传下载接口
- 添加 MediaType 导入以支持 multipart 表单数据 - 修改上传接口 consumes 属性为 MULTIPART_FORM_DATA_VALUE - 使用 @RequestPart 注解明确指定文件参数 - 将下载接口路径参数改为查询参数 path - 统一代码缩进风格为四个空格
This commit is contained in:
@@ -9,6 +9,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
@@ -38,10 +39,10 @@ public class FileController {
|
|||||||
@ApiResponse(responseCode = "200", description = "上传成功",
|
@ApiResponse(responseCode = "200", description = "上传成功",
|
||||||
content = @Content(mediaType = "application/json",
|
content = @Content(mediaType = "application/json",
|
||||||
schema = @Schema(implementation = R.class)))
|
schema = @Schema(implementation = R.class)))
|
||||||
@PostMapping
|
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
public R<FileInfo> upload(
|
public R<FileInfo> upload(
|
||||||
@Parameter(description = "要上传的文件", required = true, name = "file")
|
@Parameter(description = "要上传的文件", required = true, name = "file")
|
||||||
MultipartFile file) {
|
@RequestPart("file") MultipartFile file) {
|
||||||
try {
|
try {
|
||||||
FileInfo upload = fileUploadService.upload(file);
|
FileInfo upload = fileUploadService.upload(file);
|
||||||
return R.success(upload);
|
return R.success(upload);
|
||||||
@@ -54,10 +55,10 @@ public class FileController {
|
|||||||
@Operation(summary = "下载文件", description = "根据文件路径下载文件")
|
@Operation(summary = "下载文件", description = "根据文件路径下载文件")
|
||||||
@ApiResponse(responseCode = "200", description = "下载成功")
|
@ApiResponse(responseCode = "200", description = "下载成功")
|
||||||
@ApiResponse(responseCode = "404", description = "文件未找到")
|
@ApiResponse(responseCode = "404", description = "文件未找到")
|
||||||
@GetMapping("{path}")
|
@GetMapping
|
||||||
public ResponseEntity<Resource> download(
|
public ResponseEntity<Resource> download(
|
||||||
@Parameter(description = "文件路径", required = true)
|
@Parameter(description = "文件路径", required = true)
|
||||||
@PathVariable String path) {
|
@RequestParam("path") String path) {
|
||||||
Path normalize = Paths.get(path).normalize();
|
Path normalize = Paths.get(path).normalize();
|
||||||
try {
|
try {
|
||||||
Resource download = fileDownloadService.download(normalize.toString());
|
Resource download = fileDownloadService.download(normalize.toString());
|
||||||
|
|||||||
Reference in New Issue
Block a user