Java &引用;所需的MultipartFile参数';上传文件';“不存在”;有棱角和弹簧
我有一个带有angular2(2.1.0)的单页应用程序和一个带有Spring(microservice)的RESTAPI。微服务是通过jHipster(2.3)构建的。angular和微服务之间的通信工作正常 现在我想把文件从angular上传到spring 我已将必要的库作为依赖项包含在pom.xml文件中:Java &引用;所需的MultipartFile参数';上传文件';“不存在”;有棱角和弹簧,java,spring,spring-mvc,angular,jhipster,Java,Spring,Spring Mvc,Angular,Jhipster,我有一个带有angular2(2.1.0)的单页应用程序和一个带有Spring(microservice)的RESTAPI。微服务是通过jHipster(2.3)构建的。angular和微服务之间的通信工作正常 现在我想把文件从angular上传到spring 我已将必要的库作为依赖项包含在pom.xml文件中: <dependency> <groupId>commons-fileupload</groupId> <art
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
我的REST-API方法:
/**
* Upload single file using Spring Controller
*/
@RequestMapping(value = "/uploadFile",
method = RequestMethod.POST)
@Timed
public String uploadFileHandler(@RequestHeader("Authorization") String token,
@RequestParam("uploadfile") MultipartFile file) {
System.out.println("bin in der Methode: uploadFileHandler");
if (!file.isEmpty()) {
// do something
}
}
在Angular中,我使用模块上载文件
HTML:
通过xhr对象的Typescript:
uploadFile(file: File): Promise<any> {
return new Promise((resolve, reject) => {
let xhr: XMLHttpRequest = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(JSON.parse(xhr.response));
} else {
reject(xhr.response);
}
}
};
xhr.open('POST', this.projectAPI + '/uploadFile', true);
// If I set the content-type
// I don't get an answer in spring
//xhr.setRequestHeader('Content-Type', 'multipart/form-data');
xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('id_token'));
let formData = new FormData();
console.log(file.name);
formData.append("uploadfile", file, file.name);
xhr.send(formData);
}).catch(this.handleError);
}
uploadFile(file:file):承诺{
返回新承诺((解决、拒绝)=>{
设xhr:XMLHttpRequest=newxmlhttprequest();
xhr.onreadystatechange=()=>{
if(xhr.readyState==4){
如果(xhr.status==200){
解析(JSON.parse(xhr.response));
}否则{
拒绝(xhr.响应);
}
}
};
open('POST',this.projectAPI+'/uploadFile',true);
//如果我设置内容类型
//我在春天没有得到答案
//setRequestHeader('Content-Type','multipart/formdata');
xhr.setRequestHeader('Authorization','Bearer'+localStorage.getItem('id_token'));
设formData=new formData();
console.log(文件名);
append(“uploadfile”,file,file.name);
xhr.send(formData);
}).接住(这个.把手错误);
}
上载文件时,我会收到错误消息:
[org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'uploadfile' is not present]
... <500 Internal Server Error
[org.springframework.web.bind.MissingServletRequestParameterException:所需的多部分文件参数“uploadfile”不存在]
... 我找到了解决办法
我在“WebConfigure”类中改变了配置
在角度上,我使用了Eswar的解决方案
现在它工作得很好。Thanx.检查错误是否仍然相同。这似乎是缺少的Spring配置Hipster 2.3在此状态下不支持微服务…然而,ng2的使用完全令人困惑,它有自己的http模块来处理http请求…您的客户端似乎设置不正确。我的意思是jHipster 3.xMultipartConfigFactory
现在被标记为不推荐使用。我在jHipster中也面临同样的问题,我将第二个bean添加到我的webconfig.java
中,问题仍然存在。
uploadFile(file: File): Promise<any> {
return new Promise((resolve, reject) => {
let xhr: XMLHttpRequest = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(JSON.parse(xhr.response));
} else {
reject(xhr.response);
}
}
};
xhr.open('POST', this.projectAPI + '/uploadFile', true);
// If I set the content-type
// I don't get an answer in spring
//xhr.setRequestHeader('Content-Type', 'multipart/form-data');
xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('id_token'));
let formData = new FormData();
console.log(file.name);
formData.append("uploadfile", file, file.name);
xhr.send(formData);
}).catch(this.handleError);
}
[org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'uploadfile' is not present]
... <500 Internal Server Error
@Bean(name = "commonsMultipartResolver")
public MultipartResolver multipartResolver() {
log.info("Loading the multipart resolver");
return new StandardServletMultipartResolver();
}
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("10MB");
factory.setMaxRequestSize("10MB");
return factory.createMultipartConfig();
}