我想使用Sping Boot 将图像存储为MySQL数据库中的blob。我创建了以下模型来对数据库执行crud操作。
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import org.hibernate.annotations.DynamicUpdate;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Entity
@Table(name="user")
@DynamicUpdate(true)
@AllArgsConstructor
@Data
@NoArgsConstructor
@ToString
public class Users {
@Id
@GeneratedValue
@Column(name="u_id")
private Integer userId;
@Column(name="f_nme")
private String fNme;
@Column(name="l_nme")
private String lNme;
@Column(name="email")
private String email;
@Column
@JsonIgnore
private String password;
@Column(name="cntry")
private String country;
@Column(name="type")
private String type;
@Lob
@Column(name="prof_pic")
private byte[] profPic;
public Users() {}
public Integer getId() {
return userId;
}
public void setId(Integer id) {
this.userId = id;
}
public String getfNme() {
return fNme;
}
public void setfNme(String fNme) {
this.fNme = fNme;
}
public String getlNme() {
return lNme;
}
public void setlNme(String lNme) {
this.lNme = lNme;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public byte[] getProfPicPath() {
return profPic;
}
public void setProfPicPath(byte[] profPic) {
System.out.println(profPic);
this.profPic = profPic;
System.out.println(this.profPic);
}
}
下面是我的UserDTO类。
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.web.multipart.MultipartFile;
public class UserDTO {
private String userID;
private String email;
private String password;
private String country;
private String fName;
private String lName;
private String type;
private String profPicPath;
public String getEmail() {
return email;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getProfPicPath() {
return profPicPath;
}
public void setProfPicPath(String profPicPath) {
this.profPicPath = profPicPath;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
}
下面列出了我用来更新数据库的REST API。
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.elmbridge.PropertyManagementSystem.model.UserDTO;
import com.elmbridge.PropertyManagementSystem.repository.SearchRepository;
import com.elmbridge.PropertyManagementSystem.service.JwtUserDetailsService;
@CrossOrigin(origins = "http://localhost:4200")
@RestController
public class DashboardController {
@Autowired
private JwtUserDetailsService userDetailsService;
private UserDTO userR;
@Autowired
private SearchRepository user;
@GetMapping("/country/{email}")
public ResponseEntity<String> getCountry(@PathVariable("email") String email) {
return ResponseEntity.ok(user.findCountryByEmail(email));
}
@PutMapping("/profile-picture/{email}")
public void uploadImage(@RequestParam("file") MultipartFile imageFile, @PathVariable("email") String email) {
try {
byte[] img = imageFile.getBytes();
System.out.println(img);
userDetailsService.saveImg(img, email);
System.out.println("Image saved");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
图像没有插入到数据库中。我检查了图像是否通过使用print语句接收到用户模型,发现图像接收没有问题。我已经将打算将图像保存在MySQL数据库中的字段的数据类型设置为长blob。有人能指出我这里有什么问题吗?
1条答案
按热度按时间a8jjtwal1#
你可以使用这个代码来解决你的问题。
还必须在类UserDTO中添加