在 Spring Boot 中发送多部分响应

Dar*_*ara 6 response multipart spring-boot

我正在研究在 Spring Boot 中开发的 api。现在我有一个 API,我必须在其中发送包含一个二进制文件和 xml 的响应。两者将由多部分边界分隔。那么有没有办法做到这一点?

Gan*_*ghe 5

在 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);
    }
Run Code Online (Sandbox Code Playgroud)

  • 供参考。如果你想定制你的边界。那么你可以看到[这里](/sf/ask/3178073041/)。 (2认同)
  • 如果您想流式传输您的回复。您可以在输出流中手动编写多部分主体。[此处](https://dzone.com/articles/streaming-data-with-spring-boot-restful-web-servic) 是一个演示。 (2认同)