在spring boot中发送multipart响应

时间:2017-06-03 08:33:59

标签: spring-boot response multipart

我正在研究在春季启动时开发的apis。现在我有一个API,我必须发送包含一个二进制文件和xml的响应。 两者都将由多部分边界分开。 有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

在Spring Boot中,尝试按照分段发送响应的方式进行操作。

   @RequestMapping(method = { RequestMethod.GET },value = "/multipartdata",produces=MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<MultiValueMap<String, Object>> gerMultipartData()
            throws Exception {
        MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
        formData.add("first_name",  "ganesh");
        formData.add("last_name", "patil");
        formData.add("file-data_1", new FileSystemResource("C:\Users\ganesh\img\logo.png"));
        formData.add("file-data_2", new FileSystemResource("C:\Users\ganeshg\Desktop\download.jpg"));
        formData.add("file-data_3", new FileSystemResource("C:\Users\ganeshg\Desktop\odstext.txt"));
        formData.add("file-data_4", new FileSystemResource("D:\Agent\152845.docx"));
        formData.add("file-data_5", new FileSystemResource("D:\testxls.xlsx"));
        return new ResponseEntity<MultiValueMap<String, Object>>(formData, HttpStatus.OK);
    }