【发布时间】:2016-01-25 09:32:51
【问题描述】:
我正在尝试在我的 Restful Spring Boot 应用程序中实现 pdf 文件上传。
我有以下方法;
@RequestMapping(value = FILE_URL, method = RequestMethod.POST)
public ResponseDTO submitPDF(
@ModelAttribute("file") FileDTO file) {
MediaType mediaType = MediaType.parseMediaType(file.getFile().getContentType());
System.out.println(file.getFile().getContentType());
System.out.println(mediaType);
System.out.println(mediaType.getType());
if(!"application/pdf".equals(mediaType.getType())) {
throw new IllegalArgumentException("Incorrect file type, PDF required.");
}
... more code here ...
}
FileDTO 只是 MultipartFile 的一个包装器。
然后我使用 Postman 通过 form-data body 'file'=<filename.pdf> 发布请求
上面 printlns 中的内容类型是 ALWAYS octet-stream。无论我发送什么类型的文件(png、pdf 等),它始终是八位字节流。如果我在 Postman 中专门将 application/pdf 设置为 Content-Type 标头,则 FileDTO 中的 MultipartFile 最终为 null。
问题是,我的 Spring Controller 方法有问题,还是 Postman 没有正确构建请求?
如果 Postman 无法正确获取 Content-Type,我是否可以期望实际的客户端应用程序将内容类型正确设置为 pdf?
【问题讨论】:
标签: java spring spring-mvc postman spring-rest