java - JAVA:上传多部分文件
问题描述
我正在为 Spring 控制器中的多部分文件上传而苦苦挣扎。我已经阅读了多个问题,谷歌,但似乎没有任何效果。
我明白了
error: "Bad Request"
exception: "org.springframework.web.multipart.support.MissingServletRequestPartException"
message: "Required request part 'file' is not present"
我的 BE 控制器:
@RequestMapping(value = "/zip", method = RequestMethod.POST)
public void readFile(@RequestParam("file") MultipartFile file) throws IOException {
// code
}
FE,角度JS:
service.getZip = function getZip(file) {
var formData = new FormData();
formData.append('file', file);
return $http({
method: 'POST',
url: CONSTANTS.readFile,
data: formData,
headers: {'Content-Type': undefined}
}) .then(function (response) {
var data = response.data;
return data.id;
});
}
HTML:
<input type="file" id="file" name="file" accept=".txt"/>
application.properties 还包含:
spring.http.multipart.enabled=false
更新:
遵循@Byeon0gam 建议从我的控制器中删除@RequestParam 时,我不再收到该错误,但是当涉及到控制器时,我的文件为空。尽管在 FE 服务中,正如我所见,它不是空的:
解决方案
将您的 FE 中的 Content-Type 更改为:
headers: {'Content-Type': 'x-www-form-urlencoded'}
希望它对你有用。
推荐阅读
- php - 如果从最高顺序选择了一个值,PHP foreach 循环仅显示选定的选项
- php - 如何从数据库中获取作为模型codeigniter中的数组的数据
- javascript - 有没有办法通过使用 redux 和 react-router 更改 url 来强制更新组件?
- php - 传递给模态时显示php隐藏行
- c# - Azure blob DownloadToStream 指定本地文件名
- php - 将传感器数据从 nodemcu 发送到 PHP MySQL
- html - 如何在不越界的情况下将鼠标悬停或下拉以在同一列中显示我的列表?
- amazon-web-services - 尽管通过带有 EIP 的 NAT 网关进行路由,AWS Lambda 仍没有接收到静态 IP
- sql - SSIS 2017“执行 SQL 任务”失败找不到存储过程
- javascript - 从另一个页面访问功能(IE11)