• Spring——》feign下载文件


    推荐链接:
        总结——》【Java】
        总结——》【Mysql】
        总结——》【Spring】
        总结——》【SpringBoot】
        总结——》【MyBatis、MyBatis-Plus】

    一、服务提供者:good-house-push

    1、ITmallSyncEsfCommunityController

    方法的返回值一定是void,不要指定返回内容

    public interface ITmallSyncEsfCommunityController {
    
        @ApiOperation(value = "根据批次号,下载同步二手房小区的错误日志列表")
        @GetMapping("/api/tmall/syncEsfCommunity/logList/downloadError/{batchNo}")
        void downloadErrorLogListByBatchNo(@PathVariable(name = "batchNo") String batchNo, HttpServletResponse response);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、TmallEsfCommunitySyncController

    @Slf4j
    @Api(value = "同步二手房小区", tags = "同步二手房小区")
    @RestController
    public class TmallEsfCommunitySyncController implements ITmallSyncEsfCommunityController {
    
        @Autowired
        IEsfCommunitySyncService esfCommunitySyncService;
    
        @Override
        public void downloadErrorLogListByBatchNo(String batchNo, HttpServletResponse response) {
            esfCommunitySyncService.downloadErrorLogListByBatchNo(batchNo, response);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3、IEsfCommunitySyncService

    public interface IEsfCommunitySyncService {
        void downloadErrorLogListByBatchNo(String batchNo, HttpServletResponse response);
    }
    
    • 1
    • 2
    • 3

    4、EsfCommunitySyncService

    	@Slf4j
        @Service
        public class EsfCommunitySyncServiceImpl implements IEsfCommunitySyncService {
            @Override
            public void downloadErrorLogListByBatchNo(String batchNo, HttpServletResponse response) {
                List<EsfCommunitySyncLog> list = esfCommunitySyncLogService.selectList(new EsfCommunitySyncLog().setBatchNo(batchNo).setStatus(0));
                if (CollectionUtils.isEmpty(list)) {
                    return;
                }
                List<EsfCommunitySyncLogDownloadRes> result = BeanCopyUtil.copyListProperties(list, EsfCommunitySyncLogDownloadRes::new);
                HttpServletResponseUtils.downloadExcel(response, EsfCommunitySyncLogDownloadRes.class, result, batchNo + ".xlsx", null);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    5、HttpServletResponseUtils

    public class HttpServletResponseUtils {
        public static void downloadExcel(HttpServletResponse response, Class clazz, Collection collection, String fileName, String sheetName) {
    
            Assert.notNull(response, "response 为空");
            Assert.notNull(clazz, "clazz 为空");
            Assert.notEmpty(collection, "collection 为空");
            Assert.hasText(fileName, "fileName 为空");
    
            if (!StringUtils.hasText(sheetName)) {
                sheetName = "sheet1";
            }
    
            // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
    
    
            try {
                // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
                String urlEncoderfileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
    
                response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + urlEncoderfileName + ".xlsx");
                EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(collection);
            } catch (IOException e) {
                log.error("downloadExcel 报错, 报错信息:{}", e);
                throw new RuntimeException(e);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    二、服务消费者:house-platform-api

    1、ITmallSyncEsfCommunityApi

    方法的返回值设置为Response

    @FeignClient(value = "${feign.client.config.good-house-push.name}", url = "${feign.client.config.good-house-push.url}", path = "${feign.client.inner.path}", configuration = CatFeignConfiguration.class)
    public interface ITmallSyncEsfCommunityApi{
    
        @ApiOperation("根据批次号,下载同步二手房小区的错误日志列表")
        @GetMapping({"/api/tmall/syncEsfCommunity/logList/downloadError/{batchNo}"})
        Response downloadErrorLogListByBatchNo(@PathVariable(name = "batchNo") String batchNo);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2、TmallCityOpenController

    将服务提供者的文件下载响应的响应体(文件内容)复制到服务消费者对外的文件下载响应体中

    @Slf4j
    @Api(tags = "天猫城市开通管理")
    @RestController
    @RequestMapping(value = "/tmall", produces = "application/json;charset=utf-8")
    public class TmallController {
    
        @Autowired
        private ITmallSyncEsfCommunityApi tmallSyncEsfCommunityApi;
        
        @ApiOperation(value = "根据批次号,下载同步二手房小区的错误日志列表")
        @GetMapping("/cityopen/syncEsfCommunity/logList/downloadError/{batchNo}")
        void downloadErrorLogListByBatchNo(@PathVariable(name = "batchNo") String batchNo, HttpServletResponse response) {
    
            InputStream inputStream = null;
            try {
                Response serviceResponse = tmallSyncEsfCommunityApi.downloadErrorLogListByBatchNo(batchNo);
                Response.Body body = serviceResponse.body();
                inputStream = body.asInputStream();
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                response.setCharacterEncoding("utf-8");
                response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + batchNo + ".xlsx");
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
                int length = 0;
                byte[] temp = new byte[1024 * 10];
                while ((length = bufferedInputStream.read(temp)) != -1) {
                    bufferedOutputStream.write(temp, 0, length);
                }
                bufferedOutputStream.flush();
                bufferedOutputStream.close();
                bufferedInputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    第03章 Xpath 入门
    Kafka、RabbitMQ、RocketMQ中间件的对比
    云原生FAQ
    01准备阶段 Latex相关软件安装
    二叉树题目:从中序与后序遍历序列构造二叉树
    zgc各版本信息收集统计
    Codeforces 1684 E. MEX vs DIFF
    王道数据结构第五章二叉树的遍历第13题
    PySide6应用实践 | 在PyCharm中安装、部署、启动PySide6
    1、如何抓取Modbus TCP/UDP 数据包实战
  • 原文地址:https://blog.csdn.net/weixin_43453386/article/details/127784159