Spring启动控制器 - 将Multipart和JSON上传到DTO

Ari*_*deh 23 javascript java spring-boot

我想将表单中的文件上传到Spring Boot API端点.

UI是用React编写的:

export function createExpense(formData) {
  return dispatch => {
    axios.post(ENDPOINT,
      formData, 
      headers: {
        'Authorization': //...,
        'Content-Type': 'application/json'
      }
      ).then(({data}) => {
        //...
      })
      .catch(({response}) => {
        //...
      });
    };
}

  _onSubmit = values => {
    let formData = new FormData();
    formData.append('title', values.title);
    formData.append('description', values.description);
    formData.append('amount', values.amount);
    formData.append('image', values.image[0]);
    this.props.createExpense(formData);
  }
Run Code Online (Sandbox Code Playgroud)

这是java端代码:

@RequestMapping(path = "/{groupId}", method = RequestMethod.POST)
public ExpenseSnippetGetDto create(@RequestBody ExpensePostDto expenseDto, @PathVariable long groupId, Principal principal, BindingResult result) throws IOException {
   //..
}
Run Code Online (Sandbox Code Playgroud)

但是我在java方面得到了这个例外:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;boundary=----WebKitFormBoundaryapHVvBsdZYc6j4Af;charset=UTF-8' not supported
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?类似的API端点和javascript端代码已经在运行.

注意 我已经看到了一个解决方案,它建议请求体应该有2个属性:一个是json部分,另一个是图像.我想看看它是否可以自动转换为DTO.

更新1 客户端发送的上载有效负载应转换为以下DTO:

public class ExpensePostDto extends ExpenseBaseDto {

    private MultipartFile image;

    private String description;

    private List<Long> sharers;

}
Run Code Online (Sandbox Code Playgroud)

所以你可以说它是json和multipart的混合体.

解决问题的方法是FormData在前端和ModelAttribute后端使用:

@RequestMapping(path = "/{groupId}", method = RequestMethod.POST,
        consumes = {"multipart/form-data"})
public ExpenseSnippetGetDto create(@ModelAttribute ExpensePostDto expenseDto, @PathVariable long groupId, Principal principal) throws IOException {
   //...
}
Run Code Online (Sandbox Code Playgroud)

并在前端,摆脱Content-Type它应该由浏览器本身决定,并使用FormData(标准JS).那应该可以解决问题.

Usa*_*jad 24

是的,您可以通过包装类来完成.

1)创建一个Class以保存表单数据

public class FormWrapper {
    private MultipartFile image;
    private String title;
    private String description;
}
Run Code Online (Sandbox Code Playgroud)

2)创建Form数据

<form method="POST" enctype="multipart/form-data" id="fileUploadForm" action="link">
    <input type="text" name="title"/><br/>
    <input type="text" name="description"/><br/><br/>
    <input type="file" name="image"/><br/><br/>
    <input type="submit" value="Submit" id="btnSubmit"/>
</form>
Run Code Online (Sandbox Code Playgroud)

3)创建一个接收表单text数据和multipart文件的方法

@PostMapping("/api/upload/multi/model")
    public ResponseEntity<?> multiUploadFileModel(@ModelAttribute FormWrapper model) {
        try {
            saveUploadedFile(model.getImage());
            formRepo.save(mode.getTitle(),model.getDescription()); //Save as you want as per requirement 
        } catch (IOException e) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity("Successfully uploaded!", HttpStatus.OK);
    }
Run Code Online (Sandbox Code Playgroud)

4)保存方法 file

private void saveUploadedFile(MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
            Files.write(path, bytes);
        }
    }
Run Code Online (Sandbox Code Playgroud)


GSS*_*ain 9

我使用纯JS和Spring Boot创建了类似的东西。这是仓库。 我正在UserJSONFile作为multipart/form-data请求的一部分发送对象。

相关片段如下

Controller代码

@RestController
public class FileUploadController {

    @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = { "multipart/form-data" })
    public void upload(@RequestPart("user") @Valid User user,
            @RequestPart("file") @Valid @NotNull @NotBlank MultipartFile file) {
            System.out.println(user);
            System.out.println("Uploaded File: ");
            System.out.println("Name : " + file.getName());
            System.out.println("Type : " + file.getContentType());
            System.out.println("Name : " + file.getOriginalFilename());
            System.out.println("Size : " + file.getSize());
    }

    static class User {
        @NotNull
        String firstName;
        @NotNull
        String lastName;

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        @Override
        public String toString() {
            return "User [firstName=" + firstName + ", lastName=" + lastName + "]";
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

HTMLJS代码

<html>    
<head>
    <script>
        function onSubmit() {

            var formData = new FormData();

            formData.append("file", document.forms["userForm"].file.files[0]);
            formData.append('user', new Blob([JSON.stringify({
                "firstName": document.getElementById("firstName").value,
                "lastName": document.getElementById("lastName").value
            })], {
                    type: "application/json"
                }));
            var boundary = Math.random().toString().substr(2);
            fetch('/upload', {
                method: 'post',
                body: formData
            }).then(function (response) {
                if (response.status !== 200) {
                    alert("There was an error!");
                } else {
                    alert("Request successful");
                }
            }).catch(function (err) {
                alert("There was an error!");
            });;
        }
    </script>
</head>

<body>
    <form name="userForm">
        <label> File : </label>
        <br/>
        <input name="file" type="file">
        <br/>
        <label> First Name : </label>
        <br/>
        <input id="firstName" name="firstName" />
        <br/>
        <label> Last Name : </label>
        <br/>
        <input id="lastName" name="lastName" />
        <br/>
        <input type="button" value="Submit" id="submit" onclick="onSubmit(); return false;" />
    </form>
</body>    
</html>
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用。只是添加,需要添加:processData: false、contentType: false、cache: false,这样才能正常工作。春季启动 2.1.7。并且没有必要添加“消耗”。 (3认同)

vis*_*vis 6

我有一个类似的用例,我有一些 JSON 数据和图像上传(将其视为尝试使用个人详细信息和个人资料图像进行注册的用户)。

参考@Stephan 和@GSSwain 的回答,我想出了一个使用 Spring Boot 和 AngularJs 的解决方案。

下面是我的代码的快照。希望它可以帮助某人。

    var url = "https://abcd.com/upload";
    var config = {
        headers : {
            'Content-Type': undefined
        }

    }
    var data = {
        name: $scope.name,
        email: $scope.email
    }
    $scope.fd.append("obj", new Blob([JSON.stringify(data)], {
                type: "application/json"
            }));

    $http.post(
        url, $scope.fd,config
    )
        .then(function (response) {
            console.log("success", response)
            // This function handles success

        }, function (response) {
            console.log("error", response)
            // this function handles error

        });
Run Code Online (Sandbox Code Playgroud)

和 SpringBoot 控制器:

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = {   "multipart/form-data" })
@ResponseBody
public boolean uploadImage(@RequestPart("obj") YourDTO dto, @RequestPart("file") MultipartFile file) {
    // your logic
    return true;
}
Run Code Online (Sandbox Code Playgroud)