无法在 spring 引导中上传文件
Can't upload files in spring boot
过去 3 天我一直在为这个问题苦苦挣扎,当我尝试在我的 spring 引导项目中上传文件时,我不断收到以下异常。
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present
我不确定这是否会有所不同,但我正在将我的应用程序作为 war 部署到 weblogic 上,
这是我的控制器
@PostMapping
public AttachmentDto createAttachment(@RequestParam(value = "file") MultipartFile file) {
logger.info("createAttachment - {}", file.getOriginalFilename());
AttachmentDto attachmentDto = null;
try {
attachmentDto = attachmentService.createAttachment(new AttachmentDto(file, 1088708753L));
} catch (IOException e) {
e.printStackTrace();
}
return attachmentDto;
}
我可以在 spring 引导执行器中看到多部分 bean
在 chrome
中看到的负载
你可以尝试使用@RequestPart
,因为它使用HttpMessageConverter
,考虑到请求部分的'Content-Type' header。
Note that @RequestParam annotation can also be used to associate the part of a "multipart/form-data" request with a method argument supporting the same method argument types. The main difference is that when the method argument is not a String, @RequestParam relies on type conversion via a registered Converter or PropertyEditor while @RequestPart relies on HttpMessageConverters taking into consideration the 'Content-Type' header of the request part. @RequestParam is likely to be used with name-value form fields while @RequestPart is likely to be used with parts containing more complex content (e.g. JSON, XML).
代码:
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public AttachmentDto createAttachment(@RequestPart("file") MultipartFile file) {
logger.info("Attachment - {}", file.getOriginalFilename());
try {
return attachmentService.createAttachment(new AttachmentDto(file, 1088708753L));
} catch (final IOException e) {
logger.e("Error creating attachment", e);
}
return null;
}
@RequestParm 需要名称属性 'file'
<input type="file" class="file" name="file"/>
您正在使用多部分发送文件,因此无需进行太多配置即可获得所需的结果。
我有同样的要求,我的代码 运行 很好:
@RestController
@RequestMapping("/api/v2")
public class DocumentController {
private static String bucketName = "pharmerz-chat";
// private static String keyName = "Pharmerz"+ UUID.randomUUID();
@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA)
public URL uploadFileHandler(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) throws IOException {
/******* Printing all the possible parameter from @RequestParam *************/
System.out.println("*****************************");
System.out.println("file.getOriginalFilename() " + file.getOriginalFilename());
System.out.println("file.getContentType()" + file.getContentType());
System.out.println("file.getInputStream() " + file.getInputStream());
System.out.println("file.toString() " + file.toString());
System.out.println("file.getSize() " + file.getSize());
System.out.println("name " + name);
System.out.println("file.getBytes() " + file.getBytes());
System.out.println("file.hashCode() " + file.hashCode());
System.out.println("file.getClass() " + file.getClass());
System.out.println("file.isEmpty() " + file.isEmpty());
/**
BUSINESS LOGIC
Write code to upload file where you want
*****/
return "File uploaded";
}
None 以上解决方案对我有用,但当我深入挖掘时,我发现 spring 安全是罪魁祸首。即使我正在发送 CSRF 令牌,我也反复遇到 POST 不受支持的问题。当我在 google chrome 中使用开发人员工具进行检查并在网络选项卡中看到状态代码时,我才知道我收到了禁止的 403。我在 Spring 安全配置中将映射添加到 ignoredCsrfMapping,然后它绝对可以正常工作,没有任何其他缺陷。不知道为什么安全部门不允许我 post 多部分数据。 application.properties文件中需要说明的一些强制设置如下:
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB
spring.http.multipart.enabled=true
过去 3 天我一直在为这个问题苦苦挣扎,当我尝试在我的 spring 引导项目中上传文件时,我不断收到以下异常。
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present
我不确定这是否会有所不同,但我正在将我的应用程序作为 war 部署到 weblogic 上, 这是我的控制器
@PostMapping
public AttachmentDto createAttachment(@RequestParam(value = "file") MultipartFile file) {
logger.info("createAttachment - {}", file.getOriginalFilename());
AttachmentDto attachmentDto = null;
try {
attachmentDto = attachmentService.createAttachment(new AttachmentDto(file, 1088708753L));
} catch (IOException e) {
e.printStackTrace();
}
return attachmentDto;
}
我可以在 spring 引导执行器中看到多部分 bean
在 chrome
中看到的负载你可以尝试使用@RequestPart
,因为它使用HttpMessageConverter
,考虑到请求部分的'Content-Type' header。
Note that @RequestParam annotation can also be used to associate the part of a "multipart/form-data" request with a method argument supporting the same method argument types. The main difference is that when the method argument is not a String, @RequestParam relies on type conversion via a registered Converter or PropertyEditor while @RequestPart relies on HttpMessageConverters taking into consideration the 'Content-Type' header of the request part. @RequestParam is likely to be used with name-value form fields while @RequestPart is likely to be used with parts containing more complex content (e.g. JSON, XML).
代码:
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public AttachmentDto createAttachment(@RequestPart("file") MultipartFile file) {
logger.info("Attachment - {}", file.getOriginalFilename());
try {
return attachmentService.createAttachment(new AttachmentDto(file, 1088708753L));
} catch (final IOException e) {
logger.e("Error creating attachment", e);
}
return null;
}
@RequestParm 需要名称属性 'file'
<input type="file" class="file" name="file"/>
您正在使用多部分发送文件,因此无需进行太多配置即可获得所需的结果。 我有同样的要求,我的代码 运行 很好:
@RestController
@RequestMapping("/api/v2")
public class DocumentController {
private static String bucketName = "pharmerz-chat";
// private static String keyName = "Pharmerz"+ UUID.randomUUID();
@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA)
public URL uploadFileHandler(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) throws IOException {
/******* Printing all the possible parameter from @RequestParam *************/
System.out.println("*****************************");
System.out.println("file.getOriginalFilename() " + file.getOriginalFilename());
System.out.println("file.getContentType()" + file.getContentType());
System.out.println("file.getInputStream() " + file.getInputStream());
System.out.println("file.toString() " + file.toString());
System.out.println("file.getSize() " + file.getSize());
System.out.println("name " + name);
System.out.println("file.getBytes() " + file.getBytes());
System.out.println("file.hashCode() " + file.hashCode());
System.out.println("file.getClass() " + file.getClass());
System.out.println("file.isEmpty() " + file.isEmpty());
/**
BUSINESS LOGIC
Write code to upload file where you want
*****/
return "File uploaded";
}
None 以上解决方案对我有用,但当我深入挖掘时,我发现 spring 安全是罪魁祸首。即使我正在发送 CSRF 令牌,我也反复遇到 POST 不受支持的问题。当我在 google chrome 中使用开发人员工具进行检查并在网络选项卡中看到状态代码时,我才知道我收到了禁止的 403。我在 Spring 安全配置中将映射添加到 ignoredCsrfMapping,然后它绝对可以正常工作,没有任何其他缺陷。不知道为什么安全部门不允许我 post 多部分数据。 application.properties文件中需要说明的一些强制设置如下:
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB
spring.http.multipart.enabled=true