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.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
TheProfiler
  • 277
  • 3
  • 14
  • Possible duplicate of [Spring : File Upload RESTFUL Web Service](https://stackoverflow.com/questions/25884711/spring-file-upload-restful-web-service) – davioooh Dec 02 '17 at 12:03
  • No I am having a domain , the web client should send data information which include the attachment as a payload – TheProfiler Dec 02 '17 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. – Brian Dec 02 '17 at 14:19
  • Please put your client code or payload which you have receive. – Rahul Gupta Dec 02 '17 at 18:02

1 Answers1

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
zeagord
  • 2,257
  • 3
  • 17
  • 24