小叫给 2023-03-07 14:34 采纳率: 0%
浏览 85
已结题

java文件批量下载并打包,求请教

java下载多个文件,并打包,为啥我浏览器弹出来的下载链接都不是zip格式,但是如果将该文件修改拓展名为zip,下载文件又都在里面

    public static void downLoadZipAllFile(HttpServletRequest request, HttpServletResponse response, List<Map<String,String>> maps) {
        OutputStream outp = null;
        ZipOutputStream zos = null;
        BufferedInputStream bis = null;
        try {
            outp = response.getOutputStream();
            response.setContentType("text/html; charset=UTF-8"); //设置编码字符
            response.setContentType("application/octet-stream"); //设置内容类型为下载类型
//            response.setContentType("application/zip");// 定义输出类型
            String timeSuffix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            String zipFilePath = "D:/ftpHome/" + timeSuffix + "_DataFile.zip";
            String filedisplay = zipFilePath;
            if (filedisplay.indexOf("/") >= 0) {
                filedisplay = filedisplay.substring(filedisplay.lastIndexOf("/") + 1);
            } else if (filedisplay.indexOf("\\") >= 0) {
                filedisplay = filedisplay.substring(filedisplay.lastIndexOf("\\") + 1);
            }
            String agent = (String) request.getHeader("USER-AGENT");
            // firefox
            if (agent != null && agent.indexOf("MSIE") == -1) {
                String enableFileName = "=?UTF-8?B?"
                        + (new String(Base64.getBase64(filedisplay))) + "?=";
                response.setHeader("Content-Disposition",
                        "attachment; filename=" + enableFileName);
            } else {
                filedisplay = URLEncoder.encode(filedisplay, "utf-8");
                response.addHeader("Content-Disposition",
                        "attachment;filename=" + filedisplay);
            }
            try {
                File zip = new File(zipFilePath);
                if (!zip.exists()) {
                    zip.createNewFile();
                }
                zos = new ZipOutputStream(outp);
                for (Map<String, String> map : maps) {
                    String filePath = map.get("filePath");
                    String filename = map.get("fileName");
                    File file = new File(filePath);
                    if (file.exists()) {
                        zos.putNextEntry(new ZipEntry(filename));
                        bis = new BufferedInputStream(new FileInputStream(filePath));
                        byte[] b = new byte[1024 * 64];
                        int i = 0;
                        while ((i = bis.read(b)) > 0) {
                            zos.write(b, 0, i);
                        }
                        zos.closeEntry();
                        bis.close();
                        String isDelete = request.getParameter("isDelete");
                        if ("1".equals(isDelete)) {
                            file.delete();
                        }
                    } else {
                        outp.write("文件不存在!".getBytes("GBK"));
                    }
                }
                zos.close();
                bis = new BufferedInputStream(new FileInputStream(zipFilePath));
                byte[] buff = new byte[1024 * 64];
                bis.read(buff);
                outp.write(buff);//输出数据文件
                outp.flush();//释放缓存
                outp.close();
                bis.close();
            } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        bis.close();
                        bis = null;
                    }
                    if (zos != null) {
                        zos.close();
                        zos = null;
                    }
                    if (outp != null) {
                        outp.close();
                        outp = null;
                    }
                }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

然后目前下载的文件的效果如下:

img

img

img

求各位解答一下

  • 写回答

6条回答 默认 最新

  • 「已注销」 2023-03-07 15:21
    关注

    “Devil组”引证GPT后的撰写:
    将响应头中的内容类型

    application/octet-stream”
    
    

    更改为

    application/zip”
    
    
    评论

报告相同问题?

问题事件

  • 系统已结题 3月15日
  • 创建了问题 3月7日

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,