【发布时间】: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