springboot 实现ftp上的多个文件打包成zip下载

本文介绍如何使用SpringBoot从FTP服务器下载多个文件并打包成zip格式,通过代码示例展示了如何将远程文件保存到本地并创建zip输出流,最终将打包后的文件返回给前端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot 实现ftp上的多个文件打包成zip下载

废话不多说,直接开始代码,代码里有注释进行解释,如果有不明白的,可以在评论中说,我会回复。
正文如下:

将ftp文件保存到服务器本地,并得到本地文件的File对象

String baseFileName = forecast.getMainBillAttach().substring(forecast.getMainBillAttach().lastIndexOf("/") + 1,forecast.getMainBillAttach().length());
//获取文件类型,由于ftp上传中文名称的内容会乱码,所以这里将文件名转为base64,路径使用这个,想要找文件名的时候,在将这个转换回来即可
 String fileName = Hex.hexStr2Str(baseFileName);
if(fileName.indexOf(".") !=-1){
String type = fileName.substring(fileName.lastIndexOf("."),fileName.length());
String path = ZipPackerUtil.realPath+ZipPackerUtil.FILE_PACKAGE+forecast.getMainNo()+type;   //下载到服务器的路径与文件名
ZipPackerUtil.downloadFile(forecast.getMainBillAttach(), path);
fs[j++] = new File(path);

创建zip的输出流,并将上面得到的file对象,转换为输入流,将文件写入zip输出流中返回给前端

ZipOutputStream os = new ZipOutputStream(resposne.getOutputStream());
                BufferedInputStream bis = null;
                try {
                    Date date=new Date();
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
                    String dateStr=sdf.format(date);
                    resposne.setHeader("Content-Disposition", "attachment; filename="+dateStr+".zip");
                    resposne.setContentType("application/octet-stream; charset=GBK");
                    for(int i=0;i<fs.length;i++) {
                        if(fs[i]!=null){
                            FileInputStream fis = null;
                            fis = new FileInputStream(fs[i]);
                            ZipEntry z=new ZipEntry(fs[i].getName());
                            os.putNextEntry(z);
                            int len;
                            //读入需要下载的文件的内容,打包到zip文件
                            while((len = fis.read(buffer))>0) {
                                os.write(buffer,0,len);
                            }
                        }
                    }
                    os.flush();

这里注意我前端是有form形式,action访问接口,而返回值为文件,所以需要在返回前,设置头信息

resposne.setHeader("Content-Disposition", "attachment; filename="+dateStr+".zip");
                    resposne.setContentType("application/octet-stream; charset=GBK");

这个是上面下载文件时用的下载方法

public synchronized static  void downloadFile(String remoteFilePath, String localFilePath)
    {
        URL urlfile = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        File f = new File(localFilePath);
        try
        {
            urlfile = new URL(remoteFilePath);
            httpUrl = (HttpURLConnection)urlfile.openConnection();
            httpUrl.connect();
            bis = new BufferedInputStream(httpUrl.getInputStream());//
            bos = new BufferedOutputStream(new FileOutputStream(f));
            int len = 2048;
            byte[] b = new byte[len];
            while ((len = bis.read(b)) != -1)
            {
                bos.write(b, 0, len);
            }
            bos.flush();
            bis.close();
            httpUrl.disconnect();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                bis.close();
                bos.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

这是其中用到的常量,包括地址以及存储路径和文件名

   /**
     * 存放ZIP的临时文件夹
     */
    public static final String ZIP_PACKAGE = "temporary_zip";
    /**
     * 存放file文件的临时文件夹
     */
    public static final String FILE_PACKAGE = "主单文件_";
    /**
     * zip存放 根目录
     */
    public static final String realPath = "D:\\";

青春短暂,我在路上
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值