Spring MVC rest service with JSON content + multiple mutipart files

后端 未结 1 2059
深忆病人
深忆病人 2021-01-22 11:20

Need a spring rest service which needs to take input as JSON content and Multiple mutipart files.

相关标签:
1条回答
  • 2021-01-22 11:51

    Below is the approach I followed to handle JSON content + multiple multiparts in spring MVC controller

    BackEnd Implementation :

    @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();
    }
    

    Front Implementation :

    Request payload :

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

    RequestHeaders :

    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
    
    0 讨论(0)
提交回复
热议问题