我正在尝试制作一个安静的控制器来上传文件.我见过这个 并制作了这个控制器:
@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.";
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我用邮递员发送pdf:
但服务器因错误而崩溃:
.MultipartException: Current request is not a multipart request
Run Code Online (Sandbox Code Playgroud)
我再次找到了这个,并添加了一个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>
Run Code Online (Sandbox Code Playgroud)
不幸的是,它仍然抱怨同样的错误.
aba*_*hel 36
当您使用Postman进行多部分请求时,请不要在标题中指定自定义Content-Type.所以Postman中的Header选项卡应为空.邮差将确定表格数据边界.在Postman的Body选项卡中,您应该选择form-data并选择文件类型.您可以在https://github.com/postmanlabs/postman-app-support/issues/576找到相关的讨论.
Har*_*_OK 20
这发生在我身上一次:我有一个完美工作的 Postman 配置,但是,在没有更改任何内容的情况下,即使我没有Content-Type在 Postman 上手动通知,它也停止工作了;根据这个问题的答案,我尝试禁用标题并让 Postman 自动添加它,但这两个选项都不起作用。
我最终通过转到Body选项卡,将参数类型从Fileto更改为Text,然后返回到File,然后重新选择要发送的文件来解决这个问题;不知何故,这让它再次发挥作用。在这种特定情况下,也许闻起来像邮递员错误?
小智 7
看来问题在于对服务器的请求不是多部分请求。基本上,您需要修改客户端表单。例如:
<form action="..." method="post" enctype="multipart/form-data">
<input type="file" name="file" />
</form>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
小智 5
在 application.properties 中,请添加以下内容:
spring.servlet.multipart.max-file-size=128KB
spring.servlet.multipart.max-request-size=128KB
spring.http.multipart.enabled=false
Run Code Online (Sandbox Code Playgroud)
在您的 html 表单中,您需要一个 : enctype="multipart/form-data"。例如:
<form method="POST" enctype="multipart/form-data" action="/">
Run Code Online (Sandbox Code Playgroud)
希望这有帮助!
小智 5
我也遇到了与Postmanfor 相同的问题multipart。我通过执行以下步骤对其进行了修复:
Content-Type在该Headers部分中选择。Body选项卡中Postman,应选择,form-data然后选择file type。它为我工作。
| 归档时间: |
|
| 查看次数: |
61592 次 |
| 最近记录: |