![](/img/trans.png)
[英]How to include enctype="multipart/form-data" if I am not using <form> tag to upload file to database?
[英]Spring boot Multipart file upload along with json data
我想使用Spring Boot Multipart文件上传作为json主体的一部分来编写api,也想将img url保存在数据库中。 看起来像这样的请求:
------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--
请帮助我找到解决方案。
我以这种方式解决了这个问题。
我的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;
}
我的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...
}
注意:-对于数据参数,您可以在singleFileUploadWithObject方法中使用映射过程,希望对您和其他人有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.