[英]Spring MultipartException: The current request is not a multipart request
I'm trying to upload a file with AngularJS and Spring controller.我正在尝试使用 AngularJS 和 Spring 控制器上传文件。 The Angular controller looks like this:
Angular 控制器如下所示:
$scope.uploadFile=function(){
var formData=new FormData();
formData.append("file",file.files[0]);
$http.post('/content-files/upload /', file.files[0], {
transformRequest: function(data, headersGetterFunction) {
return data; // do nothing! FormData is very good!
},
headers: {'Content-Type': undefined }
})
.success(function(){
console.log('Post Succeded !');
})
.error(function(){
console.log('Post Failed .');
});
}
I also try this:我也试试这个:
var formData = new FormData();
formData.append('file',file.files[0]);
$http.post('/content-files/upload /', formData, {
headers: { 'Content-Type': undefined },
transformRequest: angular.identity
})
and the Spring controller looks like this: Spring 控制器如下所示:
@RequestMapping(value = "/content-files/upload/", method = RequestMethod.POST )
public @ResponseBody String handleFileUpload( @RequestParam("file") MultipartFile file) {
System.out.println("BrandController.uploadMultipart()");
String name=file.getName();
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name)));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + "!";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
My html Page is :我的 html 页面是:
<form enctype="multipart/form-data">
<input id="file-0a" class="file" type="file" file-model="myFile" name="myFile" />
<button ng-click="uploadFile()">upload me</button></form>
i have 2 jars in web-inf/lib:commons-upload.jar and commons-io.jar I add this in my spring configuration file:我在 web-inf/lib:commons-upload.jar 和 commons-io.jar 中有 2 个 jar 我将它添加到我的 spring 配置文件中:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="50000000"/>
</bean>
When I'm trying to upload a file, I get the error:当我尝试上传文件时,出现错误:
org.springframework.web.multipart.MultipartException: The current request is not a multipart request
When I'm trying to upload a file using MultipartHttpServletRequest in my spring function instead of Multipart i get this error:当我尝试在我的 spring 函数而不是 Multipart 中使用 MultipartHttpServletRequest 上传文件时,我收到此错误:
java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]: org.apache.catalina.connector.RequestFacade@196f5636
When i use HttpServletRequest request and i try to cast it i got a ClassCastException.当我使用 HttpServletRequest 请求并尝试投射它时,我得到了 ClassCastException。 Nothing change when i cancel enctype="multipart/form-data" from my form Tag
当我从我的表单标签中取消 enctype="multipart/form-data" 时没有任何变化
需要做的两件事: 1. headers: {'Content-Type': 'application/x-www-form-urlencoded'}
2. 传递的数据应转换为 URL 编码的字符串参考: 在此处输入链接描述
I stuck into the same problem.我陷入了同样的问题。 Below is angular code
下面是角度代码
things you should do:你应该做的事情:
- Remove the Content-Type from headers.
从标题中删除 Content-Type。
- For multi file upload use below code
对于多文件上传使用下面的代码
handleImageUpload = async (event) => {
if (event.target.files && event.target.files) {
const files = event.target.files;
_.map(files, (file) => {
//array for multiple files upload
this.imageData.push(file);
const reader = new FileReader();
//base64 encoded image for preview
reader.onload = async (event: any) => {
this.album.push(event.target.result);
};
reader.readAsDataURL(file);
});
this.uploadedImage = Promise.resolve(this.album);
}
}
In you submit handler use this code
在您提交处理程序时使用此代码
const formData: any = new FormData();
// adding multiple files
this.imageData.map((i) => {
formData.append("files", i);
});
// add objects
_.map(body,(value,key) => {
formData.append(key, value);
});
// delete headers it will be set by browser
headers = headers.delete("Content-Type");
// make a call to api
this.httpClient
.post(url, formData, { headers })
.subscribe((res) => console.log(res));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.