简体   繁体   English

Spring Boot Multipart文件与JSON数据一起上传

[英]Spring boot Multipart file upload along with json data

I want to write api using Spring boot Multipart file upload as part of json body and also want to save img url in database. 我想使用Spring Boot Multipart文件上传作为json主体的一部分来编写api,也想将img url保存在数据库中。 Requests that look like this: 看起来像这样的请求:

------WebKitFormBoundarynBsAcX7rJhOGsmfY
Content-Disposition: form-data; name="fdata"; filename="blob"
Content-Type: application/json

{"firstname":"saurabh","lastname":"mishra","mobile":"943847557"}
------WebKitFormBoundarynBsAcX7rJhOGsmfY
Content-Disposition: form-data; name="files"; filename="download.jpg"
Content-Type: image/jpeg


------WebKitFormBoundarynBsAcX7rJhOGsmfY--

Please help me to find the solution. 请帮助我找到解决方案。

I solve this issue in this way. 我以这种方式解决了这个问题。

My API Method 我的API方法

@RequestMapping(value="/filestore/{bucket-uuid}/appsport.com/singleFileUploadWithObject/{folder}",
        method = RequestMethod.POST)
@ResponseBody
public String singleFileUploadWithObject(
        @PathVariable(name="bucket-uuid", required = true) String bucketUUId,
        @PathVariable(name="folder", required = false) String folder,
        FileWithObject rawData) {
    return pingResponse;
}

My FileWithObject DTO 我的FileWithObject DTO

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "file", "files", "data" })
public class FileWithObject<T> {

    @JsonProperty("file")
    private MultipartFile file;
    @JsonProperty("files")
    private MultipartFile[] files;
    @JsonRawValue
    @JsonProperty("data")
    private T data;
    // getter/setter and other...
}

Note:- For data parameter you can use the mapping process in the singleFileUploadWithObject method hope it's help you and other 注意:-对于数据参数,您可以在singleFileUploadWithObject方法中使用映射过程,希望对您和其他人有帮助

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我不使用,如何包含 enctype=&quot;multipart/form-data&quot;<form> 标签上传文件到数据库? - How to include enctype="multipart/form-data" if I am not using <form> tag to upload file to database? 如何在 spring 引导中使用 POSTMAPPING 从“HTML 表单”创建 JSON 后将数据插入数据库? - How to insert data into database after creating JSON from “HTML form” with POSTMAPPING in spring boot? Spring 引导未在初始化时将 data.sql 文件的数据保存到 h2 数据库中 - Spring boot is not saving data of data.sql file into h2 database on initialization spring引导微服务和数据完整性的不同实例 - spring boot different instances of a microservice and data integrity Spring boot 2.0.0 多数据库(数据源) - Spring boot 2.0.0 multiple databases (data sources) Spring-boot和Hibernate:如何刷新数据? - Spring-boot and Hibernate: How to flush data? 在 Spring Boot 中优化数据获取和插入 - Optimizing data fetching and insertion in Spring Boot Spring Boot:设计一个缓存定时数据的服务 - Spring Boot: Design a service that caches timed data 数据持久性的 Spring Boot 依赖注入错误 - Error With Spring Boot Dependency Injection For Data Persistence 将初始数据导入Spring Boot应用程序 - import initial data into spring boot app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM