0

Current request is not a multipart request Spring boot error while trying to upload a Image file.

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

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

1 Answers1

0

try to change @PostMapping when MULTIPART_FORM_DATA_VALUE from import 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);
    }
}
Dmitrii B
  • 2,419
  • 3
  • 4
  • 13
  • I DID THIS, IT SHOWS org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported – Dukenukem97 Jan 22 '21 at 14:31