尝试上传文件,数组有问题
Trying to upload files, difficulties with arrays
我正在尝试上传文件,但 Multipartfiles 没有正确传递。
这是我的控制器,我正在尝试添加一些文件。只返回 @RequestParam,Arrays.asList(files) returns 一个空数组。我将在下面添加打印行的结果。
@Transactional
@RequestMapping(path = "/profile/files", method=RequestMethod.POST)
@ApiResponses(value = {
@ApiResponse(code = HttpServletResponse.SC_BAD_REQUEST, message = "Wrong file type", response = ErrorResponse.class),
@ApiResponse(code = HttpServletResponse.SC_FORBIDDEN, message = "Not authorized", response = ErrorResponse.class),
@ApiResponse(code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message = "Internal Error", response = InternalErrorResponse.class)
})
@ResponseBody
@ApiOperation(
value = "Add files",
tags = "Users"
)
public List<FileDto> addProfilePicture(@RequestParam("file") MultipartFile[] files) {
System.out.println("Controller: Files from params: " + files.toString());
List<MultipartFile> filesToAdd = Arrays.asList(files);
System.out.println("Controller: Files Array.asList: " + filesToAdd);
List<File> savedFiles = userService.addFiles(filesToAdd);
System.out.println("Controller: Files returned from addFiles(): " + savedFiles);
return savedFiles.stream().map(FileDto::new).collect(Collectors.toList());
}
这是服务部分:
public List<File> addFiles(List<MultipartFile> files) {
System.out.println("Service: Input files: " + files);
List<File> savedFiles = new ArrayList<>();
for(MultipartFile file: files){
File savedFile = fileService.save(file, FileService.LOCATION_DOCUMENT_FILE, S3Adapter.ACL_OWNER_ONLY);
savedFiles.add(savedFile);
}
System.out.println("Service: Saved files: " + savedFiles);
return savedFiles;
}
这是我尝试通过 Postman 上传图片时打印行为我生成的内容:
Controller: Files from params: [Lorg.springframework.web.multipart.MultipartFile;@794108b7
Controller: Files Array.asList: []
Service: Input files: []
Service: Saved files: []
Controller: Files returned from addFiles(): []
您似乎在从 POSTMAN 上传时使用了 binary
选项。此选项仅在您接收为 HttpServletRequest / MultipartRequest
.
时有效
要以 @RequestParam("files")
的形式接收文件,您需要使用 form-data
选项并将密钥作为 files
并从下拉列表中选择 file
的值,选择多个文件并提交请求。它会很好地工作。
我正在尝试上传文件,但 Multipartfiles 没有正确传递。
这是我的控制器,我正在尝试添加一些文件。只返回 @RequestParam,Arrays.asList(files) returns 一个空数组。我将在下面添加打印行的结果。
@Transactional
@RequestMapping(path = "/profile/files", method=RequestMethod.POST)
@ApiResponses(value = {
@ApiResponse(code = HttpServletResponse.SC_BAD_REQUEST, message = "Wrong file type", response = ErrorResponse.class),
@ApiResponse(code = HttpServletResponse.SC_FORBIDDEN, message = "Not authorized", response = ErrorResponse.class),
@ApiResponse(code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message = "Internal Error", response = InternalErrorResponse.class)
})
@ResponseBody
@ApiOperation(
value = "Add files",
tags = "Users"
)
public List<FileDto> addProfilePicture(@RequestParam("file") MultipartFile[] files) {
System.out.println("Controller: Files from params: " + files.toString());
List<MultipartFile> filesToAdd = Arrays.asList(files);
System.out.println("Controller: Files Array.asList: " + filesToAdd);
List<File> savedFiles = userService.addFiles(filesToAdd);
System.out.println("Controller: Files returned from addFiles(): " + savedFiles);
return savedFiles.stream().map(FileDto::new).collect(Collectors.toList());
}
这是服务部分:
public List<File> addFiles(List<MultipartFile> files) {
System.out.println("Service: Input files: " + files);
List<File> savedFiles = new ArrayList<>();
for(MultipartFile file: files){
File savedFile = fileService.save(file, FileService.LOCATION_DOCUMENT_FILE, S3Adapter.ACL_OWNER_ONLY);
savedFiles.add(savedFile);
}
System.out.println("Service: Saved files: " + savedFiles);
return savedFiles;
}
这是我尝试通过 Postman 上传图片时打印行为我生成的内容:
Controller: Files from params: [Lorg.springframework.web.multipart.MultipartFile;@794108b7
Controller: Files Array.asList: []
Service: Input files: []
Service: Saved files: []
Controller: Files returned from addFiles(): []
您似乎在从 POSTMAN 上传时使用了 binary
选项。此选项仅在您接收为 HttpServletRequest / MultipartRequest
.
要以 @RequestParam("files")
的形式接收文件,您需要使用 form-data
选项并将密钥作为 files
并从下拉列表中选择 file
的值,选择多个文件并提交请求。它会很好地工作。