Don-Luciano 2021-01-09 11:42 采纳率: 0%
浏览 127

我写了个将很多张图片打包成zip,打包完一解压,所有图片都是5kb,都看不了,有大佬帮忙看下吗?

这是我写的工具类

/**
     * 批量文件压缩下载
     *
     * @param urlMap  需要批量下载文件的链接地址列表和名称
     * @param zipName 输出的压缩包名称
     */
    public static void downZip(Map<String, String> urlMap, String zipName) {
        //响应头的设置
        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");

        String downloadName = zipName + ".zip";
        //返回客户端浏览器的版本号、类型
        String agent = request.getHeader("USER-AGENT");
        try {
            //针对IE或者以IE为内核的浏览器:
            if (agent.contains("MSIE") || agent.contains("Trident")) {
                downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");
            } else {
                //非IE浏览器的处理:
                downloadName = new String(downloadName.getBytes("UTF-8"), "ISO-8859-1");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");

        //设置压缩流:直接写入response,实现边压缩边下载
        ZipOutputStream zipos = null;
        try {
            zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
            zipos.setMethod(ZipOutputStream.DEFLATED); //设置压缩方法
        } catch (Exception e) {
            e.printStackTrace();
        }
        //循环将文件写入压缩流
        DataOutputStream os = null;

        for (Map.Entry<String, String> entry : urlMap.entrySet()) {
            String url = entry.getValue();
            try {
                URL link = new URL(url);
                //返回文件后缀名
                String suffix = FileUtil.suffix(url);
                //文件名
                String filename = entry.getKey() + suffix;
                //添加ZipEntry,并ZipEntry中写入文件流
                //这里,加上i是防止要下载的文件有重名的导致下载失败
                zipos.putNextEntry(new ZipEntry(filename));
                os = new DataOutputStream(zipos);
                InputStream is = new BufferedInputStream(link.openStream());
                byte[] b = new byte[1000];
                int length = 0;
                while ((length = is.read(b)) != -1) {
                    os.write(b, 0, length);
                }
                is.close();
                zipos.closeEntry();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //关闭流
        try {
            os.flush();
            os.close();
            zipos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这是我的controller

 public void packExp( HttpServletRequest request, HttpServletResponse response) throws IOException {

        Map<String, String> urlMap =new HashMap<>();
        urlMap.put("aaa","http://localhost:8080/profile/upload/2021/01/08/3bdbb2b6-ee96-40b6-a2be-6d1f9031a5a5.jpg");
        urlMap.put("bbb","http://localhost:8080/profile/upload/2021/01/08/ce9742bf-a690-44f7-b3cb-b64e209c1c03.jpg");
        ZipUtil.downZip(urlMap, "test");
    }

解压完生成zip文件后,一解压文件全是5kb

 而且打不开

  • 写回答

1条回答 默认 最新

  • 摆烂熊猫 2023-04-21 15:09
    关注

    加个这个试试?

    os.flush();
    os.close();
    zipos.finish();
    zipos.close();
    可能也没有用,我想是取到的文件内容不对,你图片的路径看起来是从本地Tomcat服务器里取得的,感觉取到的数据不是文件内容,而是封装过了的东西response什么的,里面可能除了文件内容 还有其他信息,所以大小都一样。。也许你应该先把取到的内容打印出来看看呢

    URL link = new URL(url);
    InputStream is = new BufferedInputStream(link.openStream());
    你在浏览器直接访问那个url,可以正常下载文件吗?

    评论 编辑记录

报告相同问题?

悬赏问题

  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备