MultipartException:当前请求不是多部分请求 [英] MultipartException: Current request is not a multipart request
问题描述
我正在尝试制作一个安静的控制器来上传文件。我见过
但是服务器崩溃了错误:
.MultipartException:当前请求不是多部分请求
我再次发现这个,并添加了 bean.xml
文件
<?xml version =1.0encoding =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>
不幸的是,仍然会抱怨同样的错误。
当您使用Postman进行多部分请求时,请不要在Header中指定自定义Content-Type。所以Postman中的Header选项卡应为空。邮差将确定表格数据边界。在Postman的Body选项卡中,您应该选择form-data并选择文件类型。您可以在 https://github.com/postmanlabs/postman-app上找到相关讨论。 -support / issues / 576
I am trying to make a restful controller to upload files. I have seen this and made this controller:
@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.";
}
}
}
and then i used postman to send a pdf:
But the server crashes with the error:
.MultipartException: Current request is not a multipart request
Again i have found this, and added a bean.xml
file
<?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>
Unfortunately, it still complains with the same error.
When you are using Postman for multipart request then don't specify a custom Content-Type in Header. So your Header tab in Postman should be empty. Postman will determine form-data boundary. In Body tab of Postman you should select form-data and select file type. You can find related discussion at https://github.com/postmanlabs/postman-app-support/issues/576
这篇关于MultipartException:当前请求不是多部分请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!