Uploading file to database using spring boot hibernate

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.


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
链接地址: http://www.djcxy.com/p/48722.html

上一篇: 使用jQuery清除<input type ='file'/>

下一篇: 使用spring boot hibernate将文件上传到数据库