How to process a multipart request consisting of a

2019-01-22 03:35发布

I have the following resource (implemented using Spring 4.05.RELEASE) which accepts a file and a JSON object:

(P.S. activityTemplate is a serializable entity class)

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

and this is the form I am testing from:

<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>

and this is the error that I get:

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

So how should I make the resource accept the JSON object as part of the multipart request, or should I be sending the form in a different way?

7条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-22 03:56

Hope this should help you. You need to set the boundary in your request to inform the HTTP Request. is simple; A brief introduction to the multipart format can be found in the below link

HTML 4.01 Specification for multipart

The following example illustrates "multipart/form-data" encoding. If the Json Object is "MyJsonObj" , and file that need to be send is "myfile.txt", the user agent might send back the following data:

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--

or if your files is of type image with name "image.gif" then,

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

...contents of image.gif...
--MyBoundary--

You specify boundary in the Content-Type header so that the server knows how to split the data sent.

So, you basically need to select a boundary value to:

  • Use a value that won't appear in the HTTP data sent to the server like 'AaB03x'.
  • Be consistent and use the same value all over the request.
查看更多
We Are One
3楼-- · 2019-01-22 04:02

The default content type is 'application/octet-stream'. Since you are uploading jar file and JSON the content type should be set in the @RequestMapping annotation as follows:

@RequestMapping(value="/create", method=RequestMethod.POST, headers="content-type=application/json,application/java-archive")
查看更多
倾城 Initia
4楼-- · 2019-01-22 04:03

this may help you, while receiving MultipartFile you should set request header content-type to "multipart/form-data" , then in your controller use consumes="multipart/form-data" , consumes also used to map our request to our method in controller.

If you want to receive JSON data , better to send request in the form of JSONString , just receive that jsonstring, later convert into json Object format then, use that object for yours operations.

check below code :

@RequestMapping(value="/savingImg", method=RequestMethod.POST, 
        consumes="multipart/form-data", produces="application/json")
public ResponseEntity<?> savingAppIMgDtlss(
        @RequestParam(value="f1", required = false) MultipartFile f1 , 
        @RequestParam(value="f2", required = false) MultipartFile f2 ,
        @RequestParam(value="f3", required = false) MultipartFile f3 ,
        @RequestParam(value="f4", required = false) MultipartFile f4 ,
        @RequestParam(value="f5", required = false) MultipartFile f5 ,
        @RequestParam(value="f6", required = false) MultipartFile f6 ,
        @RequestParam(value="f7", required = false) MultipartFile f7 ,
        @RequestParam(value="f8", required = false) MultipartFile f8 ,@RequestParam("data") String jsonString) 
                throws Exception , ParseException {
    try{
        JSONObject gstcTranObj = new JSONObject();
                //converting JSONString to JSON
        net.sf.json.JSONObject jsonDtls = net.sf.json.JSONObject.fromObject(jsonString);
        System.out.println("f1::"+f1.getOriginalFilename());
        System.out.println("f2::"+f2.getOriginalFilename());
        System.out.println("f3::"+f3.getOriginalFilename());
        System.out.println("f4::"+f4.getOriginalFilename());
        System.out.println("f5::"+f5.getOriginalFilename());
        System.out.println("f6::"+f6.getOriginalFilename());
        System.out.println("f7::"+f7.getOriginalFilename());
        System.out.println("f8::"+f8.getOriginalFilename());
} catch (Exception e) {
        e.printStackTrace();

        return new ResponseEntity<>("Failed",HttpStatus.NOT_FOUND);
    }finally{

    }
return new ResponseEntity<>("Success", HttpStatus.OK);

  }
}
查看更多
乱世女痞
5楼-- · 2019-01-22 04:12

This took me two days to work for me!

client (angular):

$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;
        }
      };

Spring (Boot):

@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
查看更多
小情绪 Triste *
6楼-- · 2019-01-22 04:13

You have not given the param names to your @RequestParts ?

public @ResponseBody ActivityTemplate createActivityTemplate(
    @RequestPart("activityTemplate") ActivityTemplate activityTemplate, @RequestPart("file") MultipartFile jarFile)
{
   //process the file and JSON
}

Note: do not forget to include the jackson mapper .jar (maps your Json to ActivityTemplate) file in your classpath.

查看更多
Lonely孤独者°
7楼-- · 2019-01-22 04:13

The error message indicates that there is no HttpMessageConverter registered for a multi-part/MIME part of content type: application/octet-stream. Still, your jarFile parameter is most likely correctly identified as application/octet-stream, so I'm assuming there's a mismatch in the parameter mapping.

So, first try setting the same name for the parameter and the form's input element.

Another problem is that the JSON is uploaded as a (regular) value of a text input in the form, not as a separate part in the multi-part/MIME. So there's no content-type header associated with it to find out that Spring should use the JSON deserializer. You can use @RequestParam instead and register a specific converter like in this answer: JSON parameter in spring MVC controller

查看更多
登录 后发表回答