Javascript 将文件从Angular客户端上载到Spring Rest

Javascript 将文件从Angular客户端上载到Spring Rest,javascript,java,spring,rest,angular,Javascript,Java,Spring,Rest,Angular,我正在尝试上载一个带有Angular的文件,并将其发送到我的rest后端Spring。对于角度服务,我使用了设置。但我有两个问题: 第一个:当我发送大的zip文件时:我的浏览器中有一个错误:net::ERR_CONNECTION_RESET;Angular似乎无法将文件发送到rest客户端 第二个:当我的zip文件不是太大时,第一个错误被忽略,但是这次,我从RESTAPI返回了这个错误:415(键入de-Support-Non-Supporté) 服务角度 Deployment(文件:文件):无

我正在尝试上载一个带有Angular的文件,并将其发送到我的rest后端Spring。对于角度服务,我使用了设置。但我有两个问题:

第一个:当我发送大的zip文件时:我的浏览器中有一个错误:
net::ERR_CONNECTION_RESET
;Angular似乎无法将文件发送到rest客户端

第二个:当我的zip文件不是太大时,第一个错误被忽略,但是这次,我从RESTAPI返回了这个错误:
415(键入de-Support-Non-Supporté)

服务角度

Deployment(文件:文件):无效{
控制台日志(“服务日志”);
让formData:formData=newformdata();
append('uploadFile',file,file.name);
let headers=新的headers();
headers.append('Content-Type','multipart/formdata')
headers.append('Accept','application/json');
let options=newrequestoptions({headers:headers});
this.http.post(URL\u API\u REST+‘上传’、formData、选项)
.map(res=>res.json())
.catch(错误=>Observable.throw(错误))
.订阅(
data=>console.log('success'),
error=>console.log(错误)
)
}
我的春假

@RequestMapping(value=“upload”,headers=(“content type=mulitpart/*”),method=RequestMethod.POST)
@应答器
公共HashMap UploaderDeployment(@RequestBody MultipartFile fichierZip){
//这里是java代码
}

有人能指出我做错了什么吗

我认为@RequestBody上传文件是错误的

试一试

public HashMap uploaderDeployment(@RequestParam(value=“file”,required=true)最终多部分文件){

public HashMap uploaderDeployment(multipartttpServletRequest请求){}

然后从
multipartttpServletRequest

获取
MultipartFile
,您可以将此代码用于文件上载,因为它在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();
}
角度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);
});

我的spring配置文件和pom.xml中缺少一些条目

pom.xml


文件上传
文件上传
1.2
公地io
公地io
2.4
在我的dispatcher-servlet.xml中


RESTAPI

public HashMap uploaderDeployment(@RequestParam MultipartFile){
//这里是java代码
}

希望它能帮助别人

我在rest控制器中的方法中添加了
consumes=MediaType.MULTIPART\u FORM\u DATA\u VALUE
,但它没有解决它添加所需的依赖项您是指MediaType的依赖项吗?如果是这样,是的,我做到了!我可以让您的teamviewer访问吗?要解决此问题,我希望您可以,但我的internet访问有一些限制。所以它不会起作用。相反,我将把我的项目推到github上,并将链接放在这里。我现在不能这样做,但我会在一段时间内这样做。我改变了我的方法:
public HashMap uploaderDeployment(multipartttpServletRequest请求){System.out.println(“something”);MultipartFile fichierZip=request.getFile(“file”);}
但仍然存在同样的问题。事实上,根本没有执行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();
}
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);
});