如何设置带注解的多部分的最大大小?

dsf9zpds  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(162)

我的目标是存储和获取个人资料图片。
如何存储:
用户将表单数据发送到邮局/帐户

  1. let formData = new FormData();
  2. formData.append("fullname", "John Wick");
  3. let profile_picture = new File(["foo"], "foo.txt", {
  4. type: "text/plain",
  5. }); // for simplicity, I use .txt
  6. formData.append("profile_picture", profile_picture);
  1. @RestController
  2. public class AccountController {
  3. @PostMapping
  4. public ResponseEntity<String> postAccount(AccountDto accountDto) {
  5. // do something
  6. }
  7. }
  8. class AccoutnDto {
  9. private String profile_picture;
  10. private String fullname;
  11. public void getProfile_Picture() {
  12. return profile_picture;
  13. }
  14. // I wish to add javax.constraints or else to prevent upload / download size larger than 128KB.
  15. public void setProfile_Picture(MultipartFile profile_picture) {
  16. this.profile_picture = profile_picture;
  17. }
  18. public void getFullname() {
  19. return fullname;
  20. }
  21. public void setFullname(String fullname) {
  22. this.fullname = fullname;
  23. }
  24. }

如何下载:
用户发送get/profile\U图片

  1. @RestController
  2. public class AccountController {
  3. @GetMapping("/profile_picture")
  4. public ResponseEntity<Resource> getAccount() {
  5. Resource file = // do something...
  6. return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
  7. "attachment; filename=\"" + file.getFilename() + "\"").body(file);
  8. }
  9. }

我目前的解决方案是在application.yml中添加一个全局配置

  1. spring.servlet.multipart:
  2. max-file-size: 128KB
  3. max-request-size: 128KB

我的预期结果是:最大文件大小和最大请求大小只适用于post/account和get/profile\u picture。
我的实际结果是:其他multipartfile对象受到影响。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题