| | |
| | | } |
| | | } |
| | | |
| | | @ApiOperation("2-0上传多个文件") |
| | | @PostMapping("/uploadFiles") |
| | | public ResponseEntity<String> uploadFiles(List<MultipartFile> files) throws IOException { |
| | | if (files.isEmpty()) { |
| | | return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件不能为空"); |
| | | } |
| | | // 上传文件路径 |
| | | String targetDir = Paths.get(uploadedFolder, "upload").toString(); |
| | | log.info("目标目录: {}", targetDir); |
| | | createDirectoriesIfNotExists(targetDir); |
| | | CompletableFuture<Void> allUploadTasks = CompletableFuture.allOf(files.stream() |
| | | .map(file -> CompletableFuture.runAsync(() -> { |
| | | try { |
| | | file.transferTo(Paths.get(targetDir, file.getOriginalFilename())); |
| | | } catch (IOException e) { |
| | | log.error("文件上传失败", e); |
| | | throw new RuntimeException("文件上传失败"); |
| | | } |
| | | })).toArray(CompletableFuture[]::new)); |
| | | try { |
| | | allUploadTasks.get(); |
| | | return ResponseEntity.ok("所有文件上传成功!上传目录为:" + targetDir); |
| | | } catch (InterruptedException | ExecutionException e) { |
| | | log.error("文件上传失败", e); |
| | | return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败"); |
| | | } |
| | | } |
| | | |
| | | |
| | | @ApiOperation("1-1 -上传单个文件") |
| | | @ApiImplicitParam(name = "filePathName", value = "文件夹名称", required = true, dataType = "String", paramType = "query", example = "upload", dataTypeClass = String.class) |
| | | @PostMapping("/uploadSingleFile") |