【问题标题】:Current request is not a multipart request : Spring boot当前请求不是多部分请求:Spring boot
【发布时间】:2021-01-22 13:25:38
【问题描述】:

当前请求不是多部分请求 Spring boot 在尝试上传图像文件时出错。

春季启动

@PostMapping(value = "/add-item")
public ResponseEntity<?> handleProductInsert ( @RequestParam MultipartFile thumbnailFile ){
  try{
          .....................
          .....................
        return new ResponseEntity("Product added successfully", HttpStatus.OK);
    }catch (Exception e){
        return new ResponseEntity("Internal Server Error. Try again later", 
          HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

反应应用

state={data:null}

   handleChange=(e)=>{
     this.setState({data:e.target.files[0]});
   }

   connectToDatabase=async()=>{
        return await axios.post(`https://localhost:8080/add-item`, this.state.data);
    }

   render()=>{
       return (<>
                <input accept="image/*" onChange={this.handleChange} type="file" />
                <button onClick={this.connectToDatabase}>Submit</button>
               </>
              )
}

【问题讨论】:

  • 尝试将consumes = { "multipart/form-data" } 添加到@PostMapping 注释
  • @ДаниилДмитроченков 我试过并得到“org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型'application/x-www-form-urlencoded'”
  • 您确定https://localhost:8080/add-item 是正确的网址吗?可能是http://localhost:8080/add-item? (http 代替 https)
  • 可能您在发送请求时遇到问题,请检查此stackoverflow.com/questions/43013858/…

标签: javascript java reactjs spring spring-boot


【解决方案1】:

尝试将@PostMapping 更改为MULTIPART_FORM_DATA_VALUEimport static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;

@PostMapping(value = "/add-item", consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> handleProductInsert ( @RequestParam MultipartFile thumbnailFile ){
  try{
          .....................
          .....................
        return new ResponseEntity("Product added successfully", HttpStatus.OK);
    }catch (Exception e){
        return new ResponseEntity("Internal Server Error. Try again later", 
          HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

【讨论】:

  • 我这样做了,它显示 org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型'application/x-www-form-urlencoded'
猜你喜欢
  • 2020-05-25
  • 2018-06-01
  • 2012-08-12
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
  • 2017-06-20
  • 1970-01-01
  • 2021-11-16
相关资源
最近更新 更多