我正在尝试使用Angular上传文件并将其发送到我的休息后端Spring。对于我的角度服务,我使用了设置here。但我遇到了两个问题:
第一个:当我发送大型zip文件时:我的浏览器出现此错误:net::ERR_CONNECTION_RESET
;似乎Angular无法将文件发送到其他客户端。
第二个:当我的zip文件不是太大时,会绕过第一个错误,但这一次,我从其余的api返回了这个错误:415 (Type de Support Non Supporté)
。
服务Angular
deployement(file: File): void {
console.log("service angular");
let formData:FormData = new FormData();
formData.append('uploadFile', file, file.name);
let headers = new Headers();
headers.append('Content-Type', 'multipart/form-data')
headers.append('Accept', 'application/json');
let options = new RequestOptions({ headers: headers });
this.http.post(URL_API_REST + 'upload', formData, options)
.map(res => res.json())
.catch(error => Observable.throw(error))
.subscribe(
data => console.log('success'),
error => console.log(error)
)
}
我的春假
@RequestMapping(value = "upload", headers = ("content-type=mulitpart/*"), method = RequestMethod.POST)
@ResponseBody
public HashMap<String, String> uploaderDeploiement(@RequestBody MultipartFile fichierZip){
//java code here
}
有谁可以指出我做错了什么?
答案 0 :(得分:0)
我认为@RequestBody对于文件上传是错误的。
尝试
public HashMap<String, String> uploaderDeploiement(@RequestParam(value = "file", required = true) final MultipartFile file){}
或者
public HashMap<String, String> uploaderDeploiement(MultipartHttpServletRequest request){}
然后从MultipartFile
MultipartHttpServletRequest
答案 1 :(得分:0)
您可以将此代码用于文件上传,因为它在myside上工作正常。
java代码
@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA)
public URL uploadFileHandler(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) throws IOException {
/******* Printing all the possible parameter from @RequestParam *************/
System.out.println("*****************************");
System.out.println("file.getOriginalFilename() " + file.getOriginalFilename());
System.out.println("file.getContentType()" + file.getContentType());
System.out.println("file.getInputStream() " + file.getInputStream());
System.out.println("file.toString() " + file.toString());
System.out.println("file.getSize() " + file.getSize());
System.out.println("name " + name);
System.out.println("file.getBytes() " + file.getBytes());
System.out.println("file.hashCode() " + file.hashCode());
System.out.println("file.getClass() " + file.getClass());
System.out.println("file.isEmpty() " + file.isEmpty());
/*************Parameters to b pass to s3 bucket put Object **************/
InputStream is = file.getInputStream();
String keyName = file.getOriginalFilename();
}
Angular js
var form = new FormData();
form.append("file", "image.jpeg");
var settings = {
"async": true,
"crossDomain": true,
"url": "http://url/",
"method": "POST",
"headers": {
"cache-control": "no-cache"
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function (response) {
console.log(response);
});
答案 2 :(得分:0)
我的spring配置文件和我的pom.xml中缺少一些条目。
的pom.xml
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
在我的dispatcher-servlet.xml
中<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="536870912"/>
</bean>
休息Api
public HashMap<String, String> uploaderDeploiement(@RequestParam MultipartFile file){
//java code here
}
希望,它会帮助别人!