| | |
| | | } |
| | | |
| | | /** |
| | | * 下载文件 |
| | | * 下载文件(路径参数方式) |
| | | */ |
| | | @GetMapping("/download/{fileName}") |
| | | @ApiOperation("下载文件") |
| | | public ResponseEntity<InputStreamResource> downloadFile( |
| | | @ApiOperation("下载文件(路径参数)") |
| | | public ResponseEntity<InputStreamResource> downloadFileByPath( |
| | | @ApiParam("文件名") @PathVariable String fileName, |
| | | @ApiParam("原始文件名") @RequestParam(required = false) String originalName) { |
| | | try { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取文件预览URL |
| | | * 下载文件(查询参数方式) |
| | | */ |
| | | @GetMapping("/download") |
| | | @ApiOperation("下载文件(查询参数)") |
| | | public ResponseEntity<InputStreamResource> downloadFile( |
| | | @ApiParam("文件名") @RequestParam String fileName, |
| | | @ApiParam("原始文件名") @RequestParam(required = false) String originalName) { |
| | | try { |
| | | InputStream inputStream = minioService.downloadFile(fileName); |
| | | |
| | | // 如果没有提供原始文件名,从路径中提取 |
| | | if (originalName == null || originalName.isEmpty()) { |
| | | String[] parts = fileName.split("/"); |
| | | originalName = parts[parts.length - 1]; |
| | | } |
| | | |
| | | // 设置响应头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); |
| | | headers.setContentDispositionFormData("attachment", |
| | | URLEncoder.encode(originalName, StandardCharsets.UTF_8.toString())); |
| | | |
| | | InputStreamResource resource = new InputStreamResource(inputStream); |
| | | |
| | | return ResponseEntity.ok() |
| | | .headers(headers) |
| | | .body(resource); |
| | | } catch (Exception e) { |
| | | log.error("文件下载失败: ", e); |
| | | return ResponseEntity.notFound().build(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取文件预览URL(路径参数方式) |
| | | */ |
| | | @GetMapping("/preview/{fileName}") |
| | | @ApiOperation("获取文件预览URL") |
| | | public Result<String> getPreviewUrl(@ApiParam("文件名") @PathVariable String fileName) { |
| | | @ApiOperation("获取文件预览URL(路径参数)") |
| | | public Result<String> getPreviewUrlByPath(@ApiParam("文件名") @PathVariable String fileName) { |
| | | try { |
| | | String previewUrl = minioService.getPreviewUrl(fileName); |
| | | return Result.success("获取预览URL成功", previewUrl); |
| | | } catch (Exception e) { |
| | | log.error("获取预览URL失败: ", e); |
| | | return Result.error("获取预览URL失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取文件预览URL(查询参数方式) |
| | | */ |
| | | @GetMapping("/preview") |
| | | @ApiOperation("获取文件预览URL(查询参数)") |
| | | public Result<String> getPreviewUrl(@ApiParam("文件名") @RequestParam String fileName) { |
| | | try { |
| | | String previewUrl = minioService.getPreviewUrl(fileName); |
| | | return Result.success("获取预览URL成功", previewUrl); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 删除文件 |
| | | * 删除文件(路径参数方式) |
| | | */ |
| | | @DeleteMapping("/delete/{fileName}") |
| | | @ApiOperation("删除文件") |
| | | public Result<Boolean> deleteFile(@ApiParam("文件名") @PathVariable String fileName) { |
| | | @ApiOperation("删除文件(路径参数)") |
| | | public Result<Boolean> deleteFileByPath(@ApiParam("文件名") @PathVariable String fileName) { |
| | | try { |
| | | minioService.deleteFile(fileName); |
| | | return Result.success("文件删除成功", true); |
| | | } catch (Exception e) { |
| | | log.error("文件删除失败: ", e); |
| | | return Result.error("文件删除失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 删除文件(查询参数方式) |
| | | */ |
| | | @DeleteMapping("/delete") |
| | | @ApiOperation("删除文件(查询参数)") |
| | | public Result<Boolean> deleteFile(@ApiParam("文件名") @RequestParam String fileName) { |
| | | try { |
| | | minioService.deleteFile(fileName); |
| | | return Result.success("文件删除成功", true); |