SpringCloud 通过feign文件传输并打zip包下载

本文介绍如何在SpringCloud项目中利用Feign从8899服务获取文件资源,然后在8800服务端进行zip打包并提供下载。详细步骤包括:1) 8899服务提供文件下载接口;2) 8800服务调用接口,处理文件并打包成zip;3) 提供下载接口http://localhost:8800/download/zip,测试下载的zip文件名按代码定义。
摘要由CSDN通过智能技术生成

一:文件打zip包并下载;

准备两个项目88008899
8899:负责提供文件的下载服务;
8800:负责去8899资源管理工程下载文件,并打zip包下载;

1):文件资源提供方8899代码

@RestController
public class DownloadFileController {
   

    private static Logger logger = Logger.getLogger(DownloadFileController.class);

    @PostMapping(value = "/download/file")
    public void downloadFile(HttpServletResponse response,@RequestParam String filePath){
   

        logger.info("文件路径:"+filePath);
		//这里模拟去资源服务器下载的过程
        File file = new File(filePath);
		//file对象转byte[],实际可能是将流对象转byte[],所以这个方法的代码根据实际情况而定;
        byte[] filebyte = this.Filebyte(file);

        try {
   
            ServletOutputStream outputStream = response.getOutputStream();
            //这里需要的就是个byte数字,实际去资源服务器下载可能会得到流对象如:inputStream,实际只需将inputSteam转为byte[]即可;
            outputStream.write(filebyte);
            outputStream.flush();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }

    /**
     * File 转 byte数组
     * @param tradeFile
     * @return
     */
    public  byte[] Filebyte(File tradeFile){
   
        byte[] buffer = null;
        try
        {
   
            FileInputStream fis = new FileInputStream(tradeFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值