我正在尝试制作一个宁静的控制器来上传文件。我已经看到了 这个 并制作了这个控制器:
@RestController
public class MaterialController {
@RequestMapping(value="/upload", method= RequestMethod.POST)
public String handleFileUpload(
@RequestParam("file") MultipartFile file){
String name = "test11";
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
}
然后我用邮递员发送了一个pdf:
但是服务器因错误而崩溃:
.MultipartException: Current request is not a multipart request
我再次找到 了这个,并添加了一个 bean.xml
文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
</beans>
不幸的是, 它仍然抱怨同样的错误。
原文由 Sal-laS 发布,翻译遵循 CC BY-SA 4.0 许可协议
当您使用 Postman 进行多部分请求时,请不要在 Header 中指定自定义 Content-Type。所以 Postman 中的 Header 选项卡应该是空的。邮递员将确定表单数据边界。在 Postman 的正文选项卡中,您应该选择表单数据并选择文件类型。您可以在 https://github.com/postmanlabs/postman-app-support/issues/576 找到相关讨论