gpt4 book ai didi

mysql - 通过 Spring Boot 将 Image 插入 MySQL 数据库

转载 作者:行者123 更新时间:2023-12-04 11:41:30 25 4
gpt4 key购买 nike

我想使用 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);
}

}

以下是我的 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();
}

}

图像未插入到数据库中。我通过使用打印语句检查图像是否接收到用户模型,发现图像接收没有问题。我已将旨在将图像保存在我的 MySQL 数据库中的字段的数据类型设置为 long blob。有人可以指出我这里有什么问题吗?

最佳答案

您可以使用此代码 .. 来解决您的问题

@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);
}
您还必须在您的类 UserDTO 中添加
private byte[] profPic;

关于mysql - 通过 Spring Boot 将 Image 插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58549508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com