Post 使用二进制和其他数据的图像
Post a image usining binairy and other data
无法将图像二进制文件添加到具有其他属性的模型中。照片模型也有标题,没有标题我可以保存图像
//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]
想通了,
在模型中将图像设置为二进制
@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);
}
无法将图像二进制文件添加到具有其他属性的模型中。照片模型也有标题,没有标题我可以保存图像
//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]
想通了, 在模型中将图像设置为二进制
@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);
}