从客户端接收上传的文件,休息

时间:2017-06-06 18:32:44

标签: java spring spring-restcontroller

我发送后端文件,这是以下上传代码:

conda.exports

现在在后端我希望通过控制器接收它,但我不知道如何映射文件属性,下面给出了null'

export class FileUploadComponent {

  @Input() multiple: boolean = false;
  @ViewChild('fileInput') inputEl: ElementRef;

  constructor(private http: Http) {}

  upload() {
    let inputEl: HTMLInputElement = this.inputEl.nativeElement;
    let fileCount: number = inputEl.files.length;
    let formData = new FormData();
    if (fileCount > 0) { // a file was selected
      for (let i = 0; i < fileCount; i++) {
        formData.append('file[]', inputEl.files.item(i));
      }
      this.http
        .post('http://localhost:8080/upload', formData).toPromise().then(() => console.log('success')).catch(() => console.log('error'));
    }
  }
}

1 个答案:

答案 0 :(得分:1)

您的方法签名不正确。

@RequestBody 注释参数映射到HTTP请求正文。

@RequestParam 注释参数映射到特定的Servlet请求参数。

使用以下内容:

public @ResponseBody String handleFileUpload(@RequestParam MultipartFile file)

如果您要发送多个,请使用数组:

public @ResponseBody String handleFileUpload(@RequestParam MultipartFile[[] file)