【问题标题】:Spring boot Jhipster image upload not workingSpring Boot Jhipster图像上传不起作用
【发布时间】:2018-09-11 12:49:56
【问题描述】:

我想从 Angular 上传图片和其他一些带有表单的东西。这是虚拟机对象:

public class UserPictureVM {

    private MultipartFile file;

    private String type;

    private String coverPosition;

这是休息方法:

 @PostMapping("/user/upload-picture")
 @Timed 
 public ResponseEntity<Void> uploadUserPicture(@RequestBody UserPictureVM userPicture){

这是提交表单的 Angular 方法:

saveCoverImage() {
    const formData: FormData = new FormData();

    formData.append('coverPosition', '0, 0');
    formData.append('file', this.coverForm.file);
    formData.append('type', 'cover');

    this.profileService.uploadImages(formData)
      .subscribe(res=> {
        console.log(res);
      })
  }

但我得到错误:

Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;charset=UTF-8' not supported

【问题讨论】:

    标签: java angular spring-boot jhipster


    【解决方案1】:

    您正在提交带有 3 个请求参数的表单。因此,您应该将控制器方法签名更改为:

      public ResponseEntity<Void> uploadUserPicture(
          @RequestParam("coverPosition") String coverPosition,
          @RequestParam("file") MultipartFile file,
          @RequestParam("type") String type)
    

    【讨论】:

    • 我试过了,但是文件是参数类型:formData,coverPosition 和类型是参数类型:查询prntscr.com/ktfcfw
    猜你喜欢
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多