sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1795 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

java下载zip文件

1.参考API可见,Java的JDK中提供一个java.util.zip的接口。其压缩过程主要是通过这两个接口压缩文件或者文件夹;

java.util.zip.ZipEntry;
java.util.zip.ZipOutputStream;

2.功能实现

1)页面请求方式:

 window.location.href = "/file/do_upload;

2) web端实现

@RequestMapping("/do_upload")
public void test(HttpServletResponse response, String taskId) throws IOException {
    long start = System.currentTimeMillis();
    // 获取附件信息
    List<File> files = fileService.listAllFiles(fileId);
    //判断是否为excel类型文件
    if(CollectionUtils.isEmpty(files)) {
        LOGGER.error("查询无关联图片或者视频");
    } else {
        // 设置文件名
        formatFileName(response, task);
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(response.getOutputStream());
            for (File srcFile : files) {
                if (StringUtils.isNotEmpty(srcFile.getFileHbasePath()) && StringUtils.isNotEmpty(srcFile.getFileName())) {
                    // 下载附件
                    byte[] bytes = fastDFSService.downloadFile(srcFile.getFileHbasePath());
                    byte[] buf = new byte[1024];
                    zos.putNextEntry(new ZipEntry(srcFile.getFileName()));
                    int len;
                    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
                    while ((len = in.read(buf)) != -1){
                        zos.write(buf, 0, len);
                    }
                    zos.closeEntry();
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("压缩完成,耗时:" + (end - start) +" ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils",e);
        }finally{
            if(zos != null){
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

设置文件名字和响应信息设置

/**
* @methodname formatFileName
* @Description {设置响应信息和格式化附件名字}
* @author admin
*/
private void formatFileName(HttpServletResponse response) throws UnsupportedEncodingException {
    String fileName = "附件文字";
    Long actualEndTime = new Date().getTime();
    fileName += actualEndTime + ".zip";
    // 获取浏览器信息
    if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
        fileName = new String(fileName.getBytes("GB2312"),"ISO-8859-1");
    } else {
        // 处理中文文件名的问题
        fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        fileName = new String(fileName.getBytes("UTF-8"), "GBK");
    }
    // 清除首部的空白行
    response.reset();
    // 设置Response容器的编码
    response.setCharacterEncoding("UTF-8");
    // 不同类型的文件对应不同的MIME类型
    response.setContentType("application/x-msdownload");
    response.setHeader("Content-Disposition","attachment; filename="+fileName);
}

参考原文地址:

获取游览器信息:https://blog.csdn.net/w410589502/article/details/73163383

压缩文件:https://blog.csdn.net/yunyingxiaoyi/article/details/103407789

https://blog.csdn.net/wjbltxx/article/details/119938101
posted on   sunny123456  阅读(1809)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示