【问题标题】:Post a image usining binairy and other data使用二进制和其他数据发布图像
【发布时间】:2020-11-11 12:32:26
【问题描述】:

无法将图像二进制文件添加到同时具有其他属性的模型中。照片模型也有标题,没有标题我可以保存图像

//payee model
     public class Payee {
                        @Id
                        private String id; 
    private String name; 
                        private Photo photo;
                    
                    //photo model
                    public class Photo {
                    
                        @Id
                        private String id;
                        private String title;
                        private Binary image;
   //Constuctor
    //Getter and Setter

                //add photo
                     @PostMapping('/photos/add')
                        public String addPhoto(@RequestBody Payee payee , @RequestParam("title") String title, @RequestParam("image") MultipartFile image, Model model)
                                throws IOException {
                            String id = photoService.addPhoto(title,image);
                            return id;
                        }][1]][1]

【问题讨论】:

  • It looks like your post is mostly code; please add some more details.。好的,我就重复三遍:D

标签: mongodb image spring-boot post upload


【解决方案1】:

想通了, 在模型中将图像设置为二进制

@RequestMapping(value = "/update", method = RequestMethod.POST, consumes = "multipart/form-data")
    public ResponseEntity<Payee> update(@RequestPart("payee") @Valid Payee payee, @RequestPart("file") @Valid MultipartFile image) throws IOException
    {
        // routine to update a payee including image
        if (image != null)
            payee.setImage(new Binary(BsonBinarySubType.BINARY, image.getBytes()));
        Payee result = payeeRepository.save(payee);
        return ResponseEntity.ok().body(result);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 2017-08-05
    • 2012-08-22
    相关资源
    最近更新 更多