java - Spring 4.3.1 中不存在必需的 MultipartFile 参数“文件”
问题描述
我正在使用 spring 4.3.1,我将使用 ng-file-upload 库上传文件。这是我的 javascript 代码,当我将 javascript 代码连接到 php 服务器时,它运行良好。
var promise = Upload.upload({
url: url + "upload",
method: 'POST',
file: file,
ignoreLoadingBar: true
}).success(function(response) {
flatForm.jsonForm = response.jsonForm;
flatForm.xmlForm = response.xmlForm;
}).error(function(response) {
$rootScope.$broadcast('veil:hide', {});
});
我在 /web-inf/lib 文件夹中附加了 commons-io-2.4.0.0.jar 和 commons-fileupload-1.3.1.jar 。e 我在 applicationContext.xml 文件中添加了 multipartResolver。
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1000000000" />
</bean>
这是我的控制器类。
@ResponseBody
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void upload(@RequestParam("file") MultipartFile file) throws Exception {
if (file == null || file.isEmpty()) {
throw new Exception("No file was sent.");
}
}
但是当我上传文件时,我得到了这样的错误。
Required MultipartFile parameter 'file' is not present
我怎样才能解决这个问题?请帮我。感谢您的观看。
解决方案
由于您指定参数文件名必须file
在控制器方法中而发生错误,但您没有在客户端代码中设置它
解决它的两种方法:
一个。删除@RequestParam("file")
以避免指定参数名称
湾。将name
属性添加到您的文件元素,如下所示:
<input type='file' name='file'>
推荐阅读
- java - Java 运行时的 exec() 方法未正确执行一个 shell 脚本,该脚本调用另一个带有重定向的 shell 脚本
- python - 以 BMP 格式保存 matplotlib 图
- python - 如何使用正则表达式删除与我需要的字符串在同一行的注释
- unity3d - 如何更改UI组超时透明?
- javascript - 在 React 中设置 Html 脚本位置
- javascript - 我的搜索栏 React 没有返回任何结果
- sql-server - 先插入外键记录以避免竞态条件?
- docker - Docker 中的 .NET 包还原与构建分开缓存
- python - 使用 Azure 存储模拟器创建 Python `BlobClient.from_blob_url`?
- c++ - 命名空间提升中没有名为 asio 的类型