Spring boot FileUpload多部分Springboot错误->所需的请求部分“文件”不存在

Spring boot FileUpload多部分Springboot错误->所需的请求部分“文件”不存在,spring-boot,file-upload,multipartform-data,multipart,spring-restcontroller,Spring Boot,File Upload,Multipartform Data,Multipart,Spring Restcontroller,我正在尝试使用Angular 4.0和SpringBoot应用程序上传一个json文件。我已经检查并尝试了Stackoverflow的其他解决方案,但我无法找出确切的问题所在 我收到400错误请求错误消息,消息为:所需的请求部分“文件”不存在 出于测试目的,我的RestController看起来像这样,但不幸的是什么都没有发生 @RestController @RequestMapping("/api") public class UploadRequestResource { .... @P

我正在尝试使用Angular 4.0和SpringBoot应用程序上传一个json文件。我已经检查并尝试了Stackoverflow的其他解决方案,但我无法找出确切的问题所在

我收到400错误请求错误消息,消息为:所需的请求部分“文件”不存在

出于测试目的,我的RestController看起来像这样,但不幸的是什么都没有发生

@RestController
@RequestMapping("/api")
public class UploadRequestResource {
....

@PostMapping("/fileupload")
@Timed
public ResponseEntity<Endpoint> FileUpload(@RequestParam("file") MultipartFile file) throws URISyntaxException {
       if (file.isEmpty()) {
          System.out.println("File is empty"); }

       System.out.println("File is not empty");

       //some logic 

       return ResponseEntity.ok() ....
    }
}
我的HTML文件如下所示:

<form name="editForm" role="form" novalidate (ngSubmit)="save()" #editForm="ngForm">
    ...
   <input type="radio" [(ngModel)]="form.uploadType"  name="uploadType" value="file">&nbsp;<label for="url">File</label><br>
   <input type="file" name="file" placeholder="Upload file..." (change)="onFileChange($event)" (focus)="onFileFocus()"/>
            </div>
        </div>
角度ts文件如下所示:

fileUpload(data): Observable<Endpoint> {
        let headers = new Headers({ 'Content-Type': 'multipart/form-data' });
        let options = new RequestOptions({ headers: headers });
        return this.http.post('/api/fileupload', data , options).map((res: Response) => {
            const jsonResponse = res.json();
            return this.convertItemFromServer(jsonResponse);
        });
    }

有人知道我应该如何解决这个错误吗?如果有任何帮助,我会非常感激的。谢谢,所以我找到了解决问题的办法。而是使用内容类型:multipart/Formdata我使用Formdata见下文

const formData = new FormData();
        formData.append('file', data, data.name);
        return this.http.post('/api/fileupload', formData).map((res: Response) => {
            const jsonResponse = res.json();
            return this.convertItemFromServer(jsonResponse);

现在它工作得很好

尝试为POST请求将enctype更新为multipart/form数据我已经为POST请求定义了enctype到multipart/form数据…我已经修改了我的初始POST…我在标签中谈到,您可以添加属性enctype
const formData = new FormData();
        formData.append('file', data, data.name);
        return this.http.post('/api/fileupload', formData).map((res: Response) => {
            const jsonResponse = res.json();
            return this.convertItemFromServer(jsonResponse);