ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

处理服务间feign 上传下载

2020-11-26 16:28:53  阅读:135  来源: 互联网

标签:map feign 处理 上传下载 public put new response


springboot版本: 2.2.9
springcloud版本: Hoxton.SR3

处理上传(服务调用端):

编码器配置

/**
 * @author Lee
 * @description feign 文件转码器
 * @date 2020/11/26 2:10 下午
 **/
public class FeignMultipartSupportConfig {
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }

    @Bean
    public feign.Logger.Level multipartLoggerLevel() {
        return feign.Logger.Level.FULL;
    }

}

feign客户端配置:

  @PostMapping(value = "schoolSchedule/importExcel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    void importExcelSchoolSchedule(@RequestPart(value = "file") MultipartFile file) throws IOException;

处理下载

服务调用端控制器

@ApiOperation(value = "学校课表管理: 导出学校课表信息模板")
@GetMapping(value = "exportTemp")
public ResponseEntity<byte[]> exportTemp() {
    Response response = eduExcelServiceFeignClient.exportTempSchoolSchedule();
    return FileUtil.handlerFileDown(response, "学校课表信息模板.xlsx");
}

服务调用端 feign

 @GetMapping(value = "schoolSchedule/exportTemp")
    Response exportTempSchoolSchedule();

服务调用端response处理工具

  /**
     * @author Lee
     * @description 处理fein 文件下载
     * @date 2020/11/26 4:08 下午
     **/
    public static ResponseEntity<byte[]> handlerFileDown(Response response, String fileName) {
        ResponseEntity<byte[]> result = null;
        InputStream inputStream = null;
        try {
            // feign文件下载
            Response.Body body = response.body();
            inputStream = body.asInputStream();
            byte[] b = new byte[inputStream.available()];
            inputStream.read(b);
            HttpHeaders heads = new HttpHeaders();
            heads.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName);
            heads.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
            result = new ResponseEntity<byte[]>(b, heads, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

服务提供端处理

public void downloadTemp(HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("所属学院", "");
        map.put("所属专业", "");
        map.put("所属班级", "");
        map.put("课程名称", "");
        map.put("教师名称", "");
        map.put("教师工号", "");
        map.put("授课日期", "");
        map.put("授课地址", "");
        map.put("节次", "");
        list.add(map);
        FileUtil.downloadExcel(list, response);
    }
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
        String tempPath = System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
        File file = new File(tempPath);
        BigExcelWriter writer = ExcelUtil.getBigWriter(file);
        // 一次性写出内容,使用默认样式,强制输出标题
        writer.write(list, true);
        //response为HttpServletResponse对象
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
        response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");
        ServletOutputStream out = response.getOutputStream();
        // 终止后删除临时文件
        file.deleteOnExit();
        writer.flush(out, true);
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

标签:map,feign,处理,上传下载,public,put,new,response
来源: https://blog.csdn.net/qq_28114645/article/details/110194555

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有