【问题标题】:How to return byte[] as multipartFile as response to UI?如何返回 byte[] 作为 multipartFile 作为对 UI 的响应?
【发布时间】:2021-11-28 23:59:00
【问题描述】:

附件已存储在数据库中。我试图实现的 Api 是将附件 Id 作为输入并将相应的 byte[] 作为 multipartFile 作为对 UI 的响应发送。

当前代码sn-p是:

@RequestMapping(method = { RequestMethod.POST }, value = "/getData", produces = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public ResponseEntity<MultiValueMap<String, Object>> getAttachmentDetails(@RequestBody Integer attachmentId) {
   MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
   byte[] result = emailIntakeService.getAttachmentDetails(attachmentId);
   formData.add("file", result);    
   return new ResponseEntity<>(formData,HttpStatus.OK);
}

响应:file=%5BB%4011a93325

有人可以帮助如何将 byte[] 作为 multipartFile 作为响应发送。

【问题讨论】:

  • 您没有说您希望它的外观如何,但是您是否尝试在将result 添加到ByteArrayResource 时将其包装到formData Map 中?

标签: java spring spring-data-jpa multipartform-data


【解决方案1】:

你可以这样试试

@RequestMapping(method = { RequestMethod.POST }, value = "/getData")
@ResponseBody
public ResponseEntity<byte[]> getAttachmentDetails(@RequestBody Integer attachmentId) {
   MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
   byte[] result = emailIntakeService.getAttachmentDetails(attachmentId);
   return ResponseEntity.ok()
        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + "filename.xls" + "\"")
        .body(result);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2023-04-06
    • 2020-05-07
    • 1970-01-01
    相关资源
    最近更新 更多