我正在尝试上传文件,但是Multipartfiles不能正确传递。
这是我要添加一些文件的控制器。仅返回@ RequestParam,Arrays.asList(files)返回一个空数组。我将在下面添加打印行的结果。
@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;
}
这是尝试通过邮递员上传图像时打印线为我产生的结果:
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(): []