ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
消费端要调用服务端的导出接口,可以采用如下两种方式。下面演示消费端模块 cloud-comsumer-feign-order80 调用服务端模块 cloud-provider-payment8001 的导出功能。 [TOC] # 1. feign接口返回byte[] 步骤如下: **1. 服务端的导出接口** ```java @RestController @RequestMapping("/order") public class OrderController { @GetMapping("/export/order") public void exportOrder(HttpServletResponse response) throws IOException { File file = new File("e:/upload/excel/poi/write/001.xlsx"); InputStream input = new FileInputStream(file); byte[] content = new byte[(int) file.length()]; input.read(content); String fileName = "order20211115.xlsx"; response.setContentType("application/msexcel;charset=utf-8"); //解决文件名中文乱码问题 fileName = URLEncoder.encode(fileName, "GB2312"); fileName = URLDecoder.decode(fileName, "ISO8859_1"); response.setHeader("content-disposition", "attachment; filename=" + fileName); OutputStream os = response.getOutputStream(); os.write(content); os.close(); } } ``` **2. feign接口返回`byte[]`** ```java @Component @FeignClient(value = "${provider.payment.name}") public interface OrderFeignService { /** * 注意:方法返回的是 byte[],不是与服务端返回一样的 void */ @RequestMapping("/order/export/order") byte[] exportOrder(); } ``` **3. 消费端的controller层** ```java @RestController @RequestMapping("/order") public class OrderFeignController { @GetMapping("/export/order") public void exportOrder(HttpServletResponse response) throws IOException { //虽然在服务端设置了response相关,但是在消费端依然需要重新设置response String fileName = "order20211115.xlsx"; response.setContentType("application/msexcel;charset=utf-8"); fileName = URLEncoder.encode(fileName, "GB2312"); fileName = URLDecoder.decode(fileName, "ISO8859_1"); response.setHeader("content-disposition", "attachment; filename=" + fileName); //将exportOrder()方法返回的 byte[] 数组写入 response 的 OutputSteam 中 response.getOutputStream().write(this.orderFeignService.exportOrder()); } } ``` **4. 访问消费端的导出接口 http://localhost/order/export/order 即可导出** <br/> # 2. feign接口返回拥有`byte[]`类型属性的实体类 步骤如下: **1. 在公共模块封装含有`byte[]`类型属性的实体类** ```java @Data public class ExportData { private String name; private byte[] content; } ``` **2. 服务端导出接口返回ExportData** ```java @RestController @RequestMapping("/order") public class OrderController { @GetMapping("/export/order2") public ExportData exportOrder2() throws IOException { ExportData exportData = new ExportData(); exportData.setName("001.xlsx"); File file = new File("e:/upload/excel/poi/write/001.xlsx"); InputStream input = new FileInputStream(file); byte[] content = new byte[(int) file.length()]; input.read(content); exportData.setContent(content); return exportData; } } ``` **3. feign接口返回ExportData** ```java @Component @FeignClient(value = "${provider.payment.name}") public interface OrderFeignService { @RequestMapping("/order/export/order2") ExportData exportOrder2(); } ``` **4. 消费端的controller层取出`byte[]`数据即可** ```java @RestController @RequestMapping("/order") public class OrderFeignController { @Autowired private OrderFeignService orderFeignService; @GetMapping("/export/order2") public void exportOrder2(HttpServletResponse response) throws IOException { ExportData data = this.orderFeignService.exportOrder2(); String fileName = data.getName(); response.setContentType("application/msexcel;charset=utf-8"); fileName = URLEncoder.encode(fileName, "GB2312"); fileName = URLDecoder.decode(fileName, "ISO8859_1"); response.setHeader("content-disposition", "attachment; filename=" + fileName); //取出byte[] response.getOutputStream().write(data.getContent()); } } ``` **5. 访问消费端的导出接口 http://localhost/order/export/order2 即可导出**