我正在嘗試使用 Spring Boot 在 MySQL 中存盤影像,我有一個用戶物體,我想在我的 FileUpload 物體之間創建一個多對一的關系。
我在前端使用 react ,這個上傳服務的目的是擁有用戶可以自己設定的個人資料圖片,但我首先想讓 User 和 FileUpload 物體之間的關系正確。
我遇到的問題是 FileUpload 表中的 joinColumn 在用戶上傳影像時不保存用戶物體 ID。它只是在外鍵欄位中回傳一個空值。
檔案上傳.java
@Entity
@Table(name="file_upload")
public class FileUpload {
@Id
@GenericGenerator(name = "uuid", strategy = "uuid2")
@GeneratedValue(generator = "uuid")
private String id;
private String name;
private String type;
@Lob
private byte[] data;
@ManyToOne
@JoinColumn( name="user_id")
private User user;
public FileUpload() {
}
public FileUpload(String name, String type, byte[] data) {
this.name = name;
this.type = type;
this.data = data;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
檔案上傳服務.java
- 我也有一個存盤庫,里面沒有自定義方法。
@Service
public class FileUploadService {
@Autowired
private FileUploadRepository fileUploadRepository;
public FileUpload store(MultipartFile file) throws IOException {
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
FileUpload fileDB = new FileUpload(fileName, file.getContentType(), file.getBytes());
return fileUploadRepository.save(fileDB);
}
public FileUpload getFile(String id) {
return fileUploadRepository.findById(id).get();
}
public Stream<FileUpload> getAllFiles() {
return fileUploadRepository.findAll().stream();
}
}
檔案上傳控制器.java
@Controller
@CrossOrigin()
public class FileUploadController {
@Autowired
private FileUploadService fileUploadService;
@PostMapping("/upload")
public ResponseEntity<?> uploadFile(@RequestParam("file")MultipartFile file) {
String message = "";
try {
fileUploadService.store(file);
message = "Uploaded the file successfully: " file.getOriginalFilename();
return ResponseEntity.status(HttpStatus.OK).body(new ApiResponse(true, message));
} catch (Exception e) {
message = "Could not upload the file: " file.getOriginalFilename() "!";
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ApiResponse(true, message));
}
}
@GetMapping("/files")
public ResponseEntity<List<?>> getListFiles() {
List<FileUploadResponse> files = fileUploadService.getAllFiles().map(dbFile -> {
String fileDownloadUri = ServletUriComponentsBuilder
.fromCurrentContextPath()
.path("/files/")
.path(dbFile.getId())
.toUriString();
return new FileUploadResponse(
dbFile.getName(),
fileDownloadUri,
dbFile.getType(),
dbFile.getData().length);
}).collect(Collectors.toList());
return ResponseEntity.status(HttpStatus.OK).body(files);
}
@GetMapping("/files/{id}")
public ResponseEntity<byte[]> getFile(@PathVariable String id) {
FileUpload fileUpload = fileUploadService.getFile(id);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" fileUpload.getName() "\"")
.body(fileUpload.getData());
}
}
郵遞員 POST 請求

MySQL 表
用戶

上傳檔案

我與確認令牌物體有相同的關系,我必須確認用戶電子郵件,該電子郵件與 ManyToOne 關系正常作業,但事實并非如此。我使用相同的關系映射,但它給了我空值,如上所示。
@ManyToOne
@JoinColumn( name="user_id")
private User user;
我可以很好地上傳檔案,但我希望能夠跟蹤用戶及其檔案。
uj5u.com熱心網友回復:
您需要為該物體分配一個用戶:
FileUpload fileDB = new FileUpload(fileName, file.getContentType(), file.getBytes());
fileDB.setUser(userThatSentImage); // you need to set user
return fileUploadRepository.save(fileDB);
你如何驗證用戶?如果您使用的是 spring security,您可以在這里找到如何從會話中檢索用戶:https : //www.baeldung.com/get-user-in-spring-security
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/375402.html
上一篇:為什么在更新資料時插入資料
