简体   繁体   English

当前请求不是多部分请求:Spring boot

[英]Current request is not a multipart request : Spring boot

Current request is not a multipart request Spring boot error while trying to upload a Image file.当前请求不是多部分请求 Spring 尝试上传图像文件时出现引导错误。

Spring Boot Spring 开机

@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);
    }
}

React App反应应用

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>
               </>
              )
}

try to change @PostMapping when MULTIPART_FORM_DATA_VALUE from import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;MULTIPART_FORM_DATA_VALUEimport static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;时尝试更改@PostMapping

@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);
    }
}

form-data requires a key along with the file in Controller. form-data 需要一个key以及 Controller 中的文件。 Try:尝试:

@PostMapping(headers = { "Content-Type=multipart/form-data" }, value = "/add-item")
public ResponseEntity<?> handleProductInsert(@RequestParam("file") 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);
    }
}

On Client Application, prepare request:在客户端应用程序上,准备请求:

<form id='form' encType="multipart/form-data" method="post">
    <input id='import-field' type="file" name="file" accept=".jpg" onChange={onUpload} />
</form>
const onUpload = event => {
    if (event.target.files.length > 0) {
        let formElement = document.getElementById('import-field');
        uploadFile(new FormData(formElement)); //do POST call here
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM