具有 JSON 内容 + 多个多部分文件的 Spring MVC Rest 服务

标签 spring rest spring-mvc service multipart

需要一个 Spring Rest 服务,该服务需要将输入作为 JSON 内容和多个多部分文件。

最佳答案

下面是我在 Spring MVC Controller 中处理 JSON 内容+多个多部分的方法

后端实现:

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes =    {"multipart/form-data"})
public
@ResponseBody
List<String> handleFileUpload(MultipartHttpServletRequest multipartHttpServletRequest) {
InputStream jsonSteam = multipartHttpServletRequest.getFile("json").getInputStream();
InputStream fileStream1 = multipartHttpServletRequest.getFile("file1").getInputStream();
InputStream fileStream2 = multipartHttpServletRequest.getFile("file2").getInputStream();
}

前端实现:

请求负载:

------WebKitFormBoundaryhKn3wrSAw57pRAso
Content-Disposition: form-data; name="file1"; filename="deleme_bkup.sql"
Content-Type: text/x-sql


------WebKitFormBoundaryhKn3wrSAw57pRAso
Content-Disposition: form-data; name="file2"; filename="source.sql"
Content-Type: text/x-sql


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


------WebKitFormBoundaryhKn3wrSAw57pRAso--
Response Headersview source

请求 header :

Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:5533
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryhKn3wrSAw57pRAso
Host:localhost:8080
Origin:http://localhost:8080
Pragma:no-cache

关于具有 JSON 内容 + 多个多部分文件的 Spring MVC Rest 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26398515/

相关文章:

python - Flask-RESTplus CORS 请求不在响应中添加 header

java - 以编程方式创建的对象可以通过注释访问Spring上下文吗?

java - org.Hibernate.AnnotationException : No Identifier Specified For Entity I don't have a id in my table

spring - Eclipse Paho Mqtt - Spring Java 配置

java - SimpleClientHttpRequestFactory 与 HttpComponentsClientHttpRequestFactory 的 Http 请求超时与 RestTemplate?

java - Spring JMX - 将包中的类注册为 MBean

java - 获取Spring MVC相对路径

java - 无法将 'logging.level.com.netflix.eureka' 下的属性绑定(bind)到 org.springframework.boot.logging.LogLevel

java - 使用 REST API 更新 QC ALM 缺陷注释部分

Python .get 嵌套 Json 值