自己用到的SpringMVC多文件下载

本文介绍了一个使用JavaScript进行文件下载的前端方法,并详细展示了后端如何收集文件路径信息,进行文件打包并指定下载位置的过程。涉及的技术包括前端的JSON请求及事件处理,后端的文件路径获取、文件打包及输出流操作。

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

js界面

function editCustomer(ipid) {
        $.getJSON("1111/2222.html", {//上传路径
            ipid : ipid
        }, function(data) {
            alert(data)

        });

    }

页面信息

<a  href="javascript:void(0)" onclick="editCustomer(2)">文件下载</a>
 

 

 

 

 

 

 

后台操作

    List<String> filePaths = new ArrayList<String>();
            
            
                    List<Object[]> fileList = f12022ServiceImpl.getFileInfo(params);//获取文件路径

            //循环得出路径加到集合里

                    if (fileList != null && fileList.size() > 0) {
                        fileName =  (String) fileList.get(0)[3];    
                        filePath =  (String) fileList.get(0)[5];
                        fileServerName =  (String) fileList.get(0)[4];
                        System.out.println(filePath+fileServerName);
                            filePaths.add(filePath+fileServerName);

                    } 
                    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            
                }
            }

//下载到那个位置
             String desPath = "C:\\Users\\Administrator\\Desktop\\DownLoad.zip";
                File zipFile = new File(desPath);
                ZipOutputStream zipStream = null;
                FileInputStream zipSource = null;
                BufferedInputStream bufferStream = null;
                try {
                    //构造最终压缩包的输出流
                    zipStream = new ZipOutputStream(new FileOutputStream(zipFile));
        for (String string : filePaths) {
                        File file = new File(string);
                        //将需要压缩的文件格式化为输入流
                        zipSource = new FileInputStream(file);
                        //压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样
                        ZipEntry zipEntry = new ZipEntry(file.getName());
                        //定位该压缩条目位置,开始写入文件到压缩包中

                        zipStream.putNextEntry(zipEntry);

                        //输入缓冲流
                        bufferStream = new BufferedInputStream(zipSource, 1024 * 10);
                        int read = 0;
                        //创建读写缓冲区
                        byte[] buf = new byte[1024 * 10];
                        while((read = bufferStream.read(buf, 0, 1024 * 10)) != -1)
                        {
                            zipStream.write(buf, 0, read);
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    //关闭流
                    try {
                        if(null != bufferStream) bufferStream.close();
                        if(null != zipStream) zipStream.close();
                        if(null != zipSource) zipSource.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                  }
          
                return null;
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值