【问题标题】:Error: you are not receiving a MultipartFile错误:您没有收到 MultipartFile
【发布时间】:2020-06-27 10:11:52
【问题描述】:

我在 java 中有一个 REST 服务,它必须接收 MultipartFile,但它给出了一个错误,它说它不是来自 angular 的 MultipartFile。我留下代码看看有没有人知道问题出在哪里...

角度 8:

sendFile(data: File): Observable<any>{
   const headers = new HttpHeaders().set('Content-Type', 'text/html; charset=utf-8');
   return this.http.post('http://localhost:8080/v1/on/file', data,{headers,responseType: 'text'})
    .pipe(
      tap(_ => this.log('send file')),
      catchError(this.handleError('not send file', []))
    );
  }

Java:

@RequestMapping("/file")
public MultipartFile filev1(
    @RequestParam("file") MultipartFile file){ 
    service.filereturn(file);
    return file;

}

【问题讨论】:

  • File 是内置类型还是你自己创建的类型?

标签: angular spring-boot rest multipart


【解决方案1】:

您必须将其作为 FormData 传递,并且不需要在请求标头中指定 Content-Type:

例如:

sendFile(data: File): Observable<any>{
   var _formData = new FormData();
   _formData.append('file', data);

   return this.http.post('http://localhost:8080/v1/on/file', _formData, { headers, responseType: 'text'})
    .pipe(
      tap(_ => this.log('send file')),
      catchError(this.handleError('not send file', []))
    );
}

【讨论】:

  • 当我返回 MultipartFile 文件时,它给了我这个错误:找不到可接受的表示,你知道如何解决这个问题吗?
  • @MariaGálvez 那个错误是来自 Java 代码还是 Angular?
猜你喜欢
  • 1970-01-01
  • 2013-06-20
  • 2011-08-08
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-24
相关资源
最近更新 更多