java文件批量下载打包成zip

1.首先html页面获取当前需要下载文件的id集合将转成字符串形式传递到后台。
。。。。。。。。。。。。。。。。
2 、下面就是获取字符串之后的批量下载

/**
     * 多文件批量下载压缩成zip
     *
     *rootPath
     *
     * */
    @RequiresPermissions(value = "/file/download")
    @RequestMapping(value = "/file/downFiles", method = RequestMethod.GET)
    public ModelAndView downFilesPost(HttpServletRequest request, HttpServletResponse response) {

        // 选中文件ID拼接的字符串
       // String idxs ="8abc83566c17147a016c177d9d100000,8abc83566c03a4d3016c03b388510004,8abc83566c03a4d3016c03b0ba170003";
        String idxs=request.getParameter("ids");
        List<String> downLoadPaths = new ArrayList<String>();//存储选中文件的下载地址
        OutputStream res = null;
        ZipOutputStream zos = null;
        String outPath;
        String lessionIdStr;
        String fileName; //浏览器下载弹出框中显示的文件名

        downLoadPaths.clear();
        String firstFileName = "";// 第一个文件的文件名
        List<mtrbTrainFileModel> fileVos = new LinkedList<mtrbTrainFileModel>();
        if (StringUtils.isNotEmpty(idxs)) {
            int end = idxs.lastIndexOf(",");
            if (end > 0) {
                if (end == idxs.length() - 1) {
                    idxs = idxs.substring(0, end);
                }
                String[] ids = idxs.split(",");
                for (int i = 0; i < ids.length; i++) {
                    mtrbTrainFileModel mtrbmodel = trainFileService.get(mtrbTrainFileModel.class, (ids[i]));
                    fileVos.add(mtrbmodel);
                    //拼接url
                    String url = rootPath + mtrbmodel.getFileUrl();
                    downLoadPaths.add(url);

  
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中实现批量下载并将文件打包ZIP格式,可以使用java.util.zip包提供的类和方法。以下是一个简单的实现示例: 首先,需要准备一个文件列表,包含要下载文件的路径。可以通过手动指定路径,或者通过程序动态获取,具体根据需求而定。 接下来,需要使用java.util.zip包中的ZipOutputStream类和FileInputStream类来实现文件打包。可以创建一个空的ZipOutputStream对象,并使用FileInputStream读取每个文件,然后将其添加到ZipOutputStream中。最后,关闭ZipOutputStream。 同时,还需要使用java.net包中的URL和URLConnection类来进行文件下载。可以为每个需要下载文件创建一个URL对象,打开URLConnection连接并获取其输入流。然后,将输入流传递给FileOutputStream,使用java.io包中的FileOutputStream类将文件下载到本地。 以下是一个简单的示例代码: import java.io.*; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipDownloader { public static void main(String[] args) { List<String> fileList = getListOfFiles(); // 获取文件列表,具体根据需求实现 String zipFileName = "downloadedFiles.zip"; // 设定打包后的ZIP文件名 try { FileOutputStream fos = new FileOutputStream(zipFileName); ZipOutputStream zos = new ZipOutputStream(fos); for (String file : fileList) { URL url = new URL(file); URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); zos.putNextEntry(new ZipEntry(file)); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) > 0) { zos.write(buffer, 0, length); } is.close(); } zos.closeEntry(); zos.close(); System.out.println("文件下载打包ZIP。"); } catch (IOException e) { e.printStackTrace(); } } } 需要注意的是,该示例只是一个简单的实现,并没有处理异常情况,如文件不存在、网络连接失败等。在实际应用中,需要对这些异常进行适当的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值