学到一寸是一寸 2021-12-31 10:12 采纳率: 66.7%
浏览 85
已结题

SpringBoot实现文件下载使用ResponseEntity<FileSystemResource>报错

SpringBoot实现文件下载使用ResponseEntity报错

函数头是

public ResponseEntity<FileSystemResource> download() {}

函数关键内容是

File file = new File(pathName);
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + fileName);
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
return  ResponseEntity
           .ok()
           .headers(headers)
           .contentLength(file.length())
           .contentType(MediaType.parseMediaType("application/octet-stream"))
           .body(new FileSystemResource(file));

报错

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class sun.nio.ch.ChannelInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: 包名.GeneralResponse["data"]->org.springframework.http.ResponseEntity["body"]->org.springframework.core.io.FileSystemResource["inputStream"])

请问如何解决?

  • 写回答

1条回答 默认 最新

  • 编号灬9527 2021-12-31 15:50
    关注

    直接用ResponseEntity<byte[]>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月13日
  • 已采纳回答 1月5日
  • 创建了问题 12月31日