1

I need to upload file in database, this is what i have tried

domain:

    @NotNull
    @Lob
    @Column(name = "data", nullable = false)
    private byte[] uploadData;

controller:

@PostMapping(value = "/uploadData" , consumes = "application/json")
    public ResponseEntity< DataInfo > uploaddata(@Valid @RequestBody DataInfo dataInfo){
        DataInfo uploadData = dataR.save(dataInfo);
        return new ResponseEntity("OK",HttpStatus.CREATED);
    }

This is the postman response

{ "timestamp": 1512210715164, "status": 415, "error": "Unsupported Media Type", "exception": "org.springframework.web.HttpMediaTypeNotSupportedException", "message": "Content type 'application/x-www-form-urlencoded' not supported", "path": "/api/uploadData" }

What I am doing wrong ? please help . Here I have to upload the file in to database. The data info contains information with a attachment.

4
  • Possible duplicate of Spring : File Upload RESTFUL Web Service Commented Dec 2, 2017 at 12:03
  • No I am having a domain , the web client should send data information which include the attachment as a payload Commented Dec 2, 2017 at 12:10
  • Your mapping is set to consumes = "application/json". You are sending application/x-www-form-urlencoded. The controller is returning an exception. What exactly are you posting? Without further information, the accepted answer to davioooh's duplicate flag would be appropriate. Commented Dec 2, 2017 at 14:19
  • Please put your client code or payload which you have receive. Commented Dec 2, 2017 at 18:02

1 Answer 1

1

Files have to be handled as a multipart data when you try sending from a client. (You can refer here to know more about the Multipart request here)

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Document> UploadFile( @RequestParam("file") MultipartFile file) {
    // Upload Logic
}

Additionally, you can specify the file size in your service properties like below:

  http:
    multipart:
      max-file-size: 10mb
      max-request-size: 12mb
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.