如何在Spring restful服务中处理由文件和JSON对象组成的多部分请求?

Sam*_*ami 19 java rest spring json multipartform-data

我有以下资源(使用Spring 4.05.RELEASE实现),它接受一个文件和一个JSON对象:

(PS activityTemplate是一个可序列化的实体类)

...
@RequestMapping(value="/create", method=RequestMethod.POST)
public @ResponseBody ActivityTemplate createActivityTemplate(
        @RequestPart ActivityTemplate activityTemplate, @RequestPart MultipartFile jarFile)
{
   //process the file and JSON
}
...
Run Code Online (Sandbox Code Playgroud)

这是我测试的形式:

<form method="POST" enctype="multipart/form-data"
    action="http://localhost:8080/activityTemplates/create">
    JSON: <input type="text" name="activityTemplate" value='/* the JSON object*/'><br />

    File to upload: <input type="file" name="file">
    <input type="submit" value="Upload">
</form>
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

 There was an unexpected error (type=Unsupported Media Type, status=415).
 Content type 'application/octet-stream' not supported
Run Code Online (Sandbox Code Playgroud)

那么如何让资源接受JSON对象作为multipart请求的一部分,或者我应该以不同的方式发送表单?

moh*_*ohi 25

这花了我两天时间为我工作!

客户(角度):

$scope.saveForm = function () {
      var formData = new FormData();
      var file = $scope.myFile;
      var json = $scope.myJson;
      formData.append("file", file);
      formData.append("ad",JSON.stringify(json));//important: convert to string JSON!
      var req = {
        url: '/upload',
        method: 'POST',
        headers: {'Content-Type': undefined},
        data: formData,
        transformRequest: function (data, headersGetterFunction) {
          return data;
        }
      };
Run Code Online (Sandbox Code Playgroud)

春天(靴子):

@RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody
    Advertisement storeAd(@RequestPart("ad") String adString, @RequestPart("file") MultipartFile file) throws IOException {

        Advertisement jsonAd = new ObjectMapper().readValue(adString, Advertisement.class);
//do whatever you want with your file and jsonAd
Run Code Online (Sandbox Code Playgroud)


Sri*_* DD 2

希望这对您有所帮助。您需要在请求中设置边界以通知 HTTP 请求。很简单;多部分格式的简要介绍可以在下面的链接中找到

HTML 4.01 多部分规范

以下示例说明了“ multipart/form-data ”编码。如果Json 对象是“ MyJsonObj ”,并且需要发送的文件是“ myfile.txt ”,则用户代理可能会发回以下数据:

Content-Type: multipart/form-data; boundary=MyBoundary

--MyBoundary
Content-Disposition: form-data; name="myJsonString"
Content-Type: application/json

MyJsonObj //Your Json Object goes here
--MyBoundary
Content-Disposition: form-data; name="files"; filename="myfile.txt"
Content-Type: text/plain

... contents of myfile.txt ...
--MyBoundary--
Run Code Online (Sandbox Code Playgroud)

或者如果您的文件是名为“ image.gif ”的图像类型,那么,

--MyBoundary
Content-Disposition: file; filename="image.gif"
Content-Type: image/gif
Content-Transfer-Encoding: binary

...contents of image.gif...
--MyBoundary--
Run Code Online (Sandbox Code Playgroud)

boundary在 中指定Content-Type header,以便服务器知道如何拆分发送的数据。

因此,您基本上需要选择一个边界值来:

  • 使用不会出现在发送到服务器的 HTTP 数据中的值,例如'AaB03x'.
  • 保持一致并在整个请求中使用相同的值。