Peg*_*008 5 mysql blob image spring-boot
我想使用Spring 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);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
下面列出了我的用于更新数据库的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();
}
}
Run Code Online (Sandbox Code Playgroud)
图像未插入数据库。我检查了是否通过使用打印语句将图像接收到用户模型,并且发现图像已正确接收。我已经设置了用于将图像保存在我的MySQL数据库中的字段的数据类型为长blob。有人可以指出我这里有什么问题吗?
小智 1
您可以使用此代码..来解决您的问题。
@PostMapping("/profile-picture")
public ResponseEntity uploadImage(@RequestParam("file") MultipartFile imageFile, @ModelAttribute UserDTO requestDto) {
try {
UserDTO created =userDetailsService.saveImg(requestDto, file.getBytes());
return new ResponseEntity<>(created, HttpStatus.OK);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
Run Code Online (Sandbox Code Playgroud)
您还必须在您的类 UserDTO 中添加
private byte[] profPic;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |