【问题标题】:Want to import image to mongo using springboot想使用spring boot将图像导入mongodb
【发布时间】:2020-11-11 15:16:50
【问题描述】:

仅创建一个模型时遇到问题,创建了照片模型和收款人模型,但想将照片模型添加到收款人模型但不接受照片的二进制部分。尝试从邮递员发布时不断收到二进制错误,知道需要更改控制器但有点卡住,任何链接或对初学者的建议将不胜感激

//Payee model
    public class Payee {
                @Id
                private String id;
            
                private Contact contact;
                private String notes;
                private String project;
                private String member;
                private Integer staffMember;
                private Photo photo;
                private BankAccount userBankAccount;
                private PayeeBankAccount payeeBankAccountList;
            

//Payee constructor
                public Payee(Contact contact, String notes, String project, String member, Integer staffMember,
                             Photo photo, BankAccount userBankAccount, PayeeBankAccount payeeBankAccountList) {
                    this.contact = contact;
                    this.notes = notes;
                    this.project = project;
                    this.member = member;
                    this.staffMember = staffMember;
                    this.photo = photo;
            
                    this.userBankAccount = userBankAccount;
                    this.payeeBankAccountList = payeeBankAccountList;
                }
            
// Getters and Setters
                public String getId() {return id;}
            
                public void setId(String id) {this.id = id;}
            
                public Contact getContact() { return contact; }
            
                public void setContact(Contact contact) { this.contact = contact; }
            
                public String getNotes() { return notes; }
            
                public void setNotes(String notes) { this.notes = notes; }
            
                public String getProject() { return project; }
            
                public void setProject(String project) { this.project = project; }
            
                public String getMember() { return member; }
            
                public void setMember(String member) { this.member = member; }
            
                public Integer getStaffMember() { return staffMember; }
            
                public void setStaffMember(Integer staffMember) { this.staffMember = staffMember; }
            
                public Photo getPhoto() {return photo;}
            
                public void setPhoto(Photo photo) {this.photo = photo;}
            
                public BankAccount getUserBankAccount() { return userBankAccount; }
            
                public void setUserBankAccount(BankAccount userBankAccount) { this.userBankAccount = userBankAccount}
            
                public PayeeBankAccount getPayeeBankAccountList() { return payeeBankAccountList; }
            
                public void setPayeeBankAccountList(PayeeBankAccount payeeBankAccountList){this.payeeBankAccountList= payeeBankAccountList; }
            
            
            }

            //photo model
            public class Photo {
            
                @Id
                private String id;
            
                private String title;
            
                private Binary image;
            
//Photo constructor
                public Photo(String id, String title, Binary image) {
                    this.id = id;
                    this.title = title;
                    this.image = image;
                }
            
                public Photo(String title) {
                    super();
                    this.title = title;
                }
            
                public String getId() {
                    return id;
                }
            
                public void setId(String id) {
                    this.id = id;
                }
            
                public String getTitle() {
                    return title;
                }
            
                public void setTitle(String title) {
                    this.title = title;
                }
            
                public Binary getImage() {
                    return image;
                }
            
                public void setImage(Binary image) {
                    this.image = image;
                }
            
                @Override
                public String toString() {
                    return "Photo \[id=" + id + ", title=" + title + ", image=" + image + "\]";
                }
            
            }
            
        //photo service
            @Service
            public class PhotoService {
            
                @Autowired
                private PhotoRepository photoRepo;
            
                public Photo getPhoto(String id) {
                    return photoRepo.findById(id).get();
                }
            
                public String addPhoto(String title, MultipartFile file) throws IOException {
                    Photo photo = new Photo(title);
                    photo.setImage(new Binary(BsonBinarySubType.BINARY, file.getBytes()));
                    photo = photoRepo.insert(photo);
                    return photo.getId();
                }
            }
            

照片和收款人 PostMapping 应该是一体的,这部分不知道去哪里 //添加收款人 @PostMapping('/addPayee') 公共无效插入(@RequestBody 收款人收款人){ this.payeeRepository.save(payee); }

        //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]
                
    
    
      [1]: https://i.stack.imgur.com/lF6KZ.png

【问题讨论】:

标签: mongodb image spring-boot upload binary


【解决方案1】:

在你的模型中发现它有二进制的图像,在你的控制器中有一些类似的东西

@RequestMapping(value = "/update", method = RequestMethod.POST, consumes = "multipart/form-data") 公共 ResponseEntity 更新(@RequestPart("payee") @Valid Payee payee, @RequestPart("file") @Valid MultipartFile image)抛出 IOException { // 更新收款人的例程,包括图片 如果(图像!= null) payee.setImage(new Binary(BsonBinarySubType.BINARY, image.getBytes())); 收款人结果 = payeeRepository.save(payee); 返回 ResponseEntity.ok().body(result); }

【讨论】:

    猜你喜欢
    • 2019-07-10
    • 2016-11-06
    • 2016-01-23
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2018-09-07
    相关资源
    最近更新 更多