我的目标是存储和获取个人资料图片。
如何存储:
用户将表单数据发送到邮局/帐户
let formData = new FormData();
formData.append("fullname", "John Wick");
let profile_picture = new File(["foo"], "foo.txt", {
type: "text/plain",
}); // for simplicity, I use .txt
formData.append("profile_picture", profile_picture);
@RestController
public class AccountController {
@PostMapping
public ResponseEntity<String> postAccount(AccountDto accountDto) {
// do something
}
}
class AccoutnDto {
private String profile_picture;
private String fullname;
public void getProfile_Picture() {
return profile_picture;
}
// I wish to add javax.constraints or else to prevent upload / download size larger than 128KB.
public void setProfile_Picture(MultipartFile profile_picture) {
this.profile_picture = profile_picture;
}
public void getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
}
如何下载:
用户发送get/profile\U图片
@RestController
public class AccountController {
@GetMapping("/profile_picture")
public ResponseEntity<Resource> getAccount() {
Resource file = // do something...
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + file.getFilename() + "\"").body(file);
}
}
我目前的解决方案是在application.yml中添加一个全局配置
spring.servlet.multipart:
max-file-size: 128KB
max-request-size: 128KB
我的预期结果是:最大文件大小和最大请求大小只适用于post/account和get/profile\u picture。
我的实际结果是:其他multipartfile对象受到影响。
暂无答案!
目前还没有任何答案,快来回答吧!