SpringCloud使用feign调用下载文件服务

这里写自定义目录标题

  • 消费者端
  • feign客户端
  • 提供者端
    • controller 层
    • service 层

消费者端

消费者端返回的类型是:ResponseEntity

/**
     * 下载手板树模板
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = {"/HandboardAction/DownTreeTemplate"}, method = {RequestMethod.GET}, produces = {"application/json"})
    public ResponseEntity<byte[]> downTreeTemplate(HttpServletResponse dresponse) throws Exception {
        System.err.println("进来了》》》》》");
        ResponseEntity<byte[]> result = null;
        // feign文件下载
        Response response = TreeService.downTreeTemplate();
        Response.Body body = response.body();
        try (InputStream inputStream = body.asInputStream();) {
            // 构建输出流,将输入流写入到输出流
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[10240];
            while (true) {
                int len = inputStream.read(buf);
                if (len < 0) {
                    break;
                }
                bos.write(buf, 0, len);
            }
			//将输出流转为字节数组,通过ResponseEntity返回
            byte[] by = bos.toByteArray();
            System.err.println("接收到的文件大小为:" + by.length);
            //设置响应头
            HttpHeaders heads = new HttpHeaders();
            heads.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + new String("text.xls".getBytes(), "ISO8859-1"));
            heads.add(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel;charset=utf-8");

            result = new ResponseEntity<byte[]>(by, heads, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.err.println("执行完毕》》》》》");
        return result;
    }

feign客户端

feign客户端的返回值需要注意,返回的类型为feign提供的Response,而不是:HttpServletResponse
import feign.Response;

/**
     * 下载模板
     * @return
     */
    @RequestMapping(value = "/downTreeTemplate",method = {RequestMethod.GET},produces = {"application/json"})
    Response downTreeTemplate();

提供者端

controller 层

@RequestMapping(value = {"/downTreeTemplate"}, method = {RequestMethod.GET})
    public void downTreeTemplate(HttpServletResponse response) {
        System.err.println("进来了》》》》》》");
        treeService.downTreeTemplate(response);
        System.err.println("执行完毕》》》》》》");
    }

service 层

/**
     * 下载手板树模板
     * 
     * @param response
     * @return
     */
    public void downTreeTemplate( HttpServletResponse response) {
        try {
            String filename = "text.xls";
            File downloadFile = new File("d:/com/"+filename);
            download(response,downloadFile,filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
/**
     * 下载处理
     *
     * @param response
     * @param downloadFile
     * @param fileName
     * @throws IOException
     */
    public static void download(final HttpServletResponse response, final File downloadFile, final String fileName) throws IOException {

        final File downloadFileTep = downloadFile;
        if (!downloadFileTep.exists()) {
            System.err.println("文件不存在》》》》》》》》》》");
            response.sendError(404, "File not found!");
            return;
        }

        try (final BufferedInputStream bufferStream = new BufferedInputStream(new FileInputStream(downloadFileTep))) {
            final byte[] buf = new byte[2048];
            int len;
            response.reset();
//            response.setContentType("application/x-msdownload");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;" +
                    "filename=" + new String(fileName.getBytes(), "ISO8859-1"));// 处理下载文件名乱码问题
            final OutputStream out = response.getOutputStream();
            int size =0;
            while ((len = bufferStream.read(buf)) > 0) {
                out.write(buf, 0, len);
                size += len;
                System.err.println("文件大小为:"+size);
            }
            bufferStream.close();
            out.close();
        }
    }

你可能感兴趣的:(springcloud)