美文网首页
springboot springMvc 支持同时上传文件和 j

springboot springMvc 支持同时上传文件和 j

作者: 爱工作的java程序猿 | 来源:发表于2020-04-09 17:24 被阅读0次

https://www.jianshu.com/p/8c8f78eb2740

后端代码

  • 其中 UserDto 为接收 json 数据的对象
  • 注解为 @RequestPart,用 @RequestParam 接收不到 json 数据
@PostMapping(value = "/uploadFileAndJson")
public void uploadFileAndJson(
                                  @RequestPart("file") MultipartFile file, 
                                  @RequestPart("userDto") UserDto userDto) {
        // 业务代码
}

前端请求

  • 这里使用 idea 的 http client
POST http://localhost:8080/uploadFileAndJson
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary--
Content-Disposition: form-data; name="userDto";
Content-Type: application/json

{"username":"admin", "password":"123456"}

--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="fileUpload.json"

< D:\Documents\Desktop\test.txt


--WebAppBoundary

相关文章

网友评论

      本文标题:springboot springMvc 支持同时上传文件和 j

      本文链接:https://www.haomeiwen.com/subject/xgacmhtx.html