private void download2(HttpServletResponse response, String zipFilePath, String filename) throws IOException { // 设置响应的内容类型为ZIP文件 response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); // 创建输入流以读取ZIP文件内容 FileInputStream fileInputStream = new FileInputStream(new File(zipFilePath)); // 获取响应输出流 javax.servlet.ServletOutputStream out = response.getOutputStream(); // 缓冲区大小 byte[] buffer = new byte[4096]; int bytesRead; // 读取ZIP文件内容并写入响应输出流 while ((bytesRead = fileInputStream.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } // 关闭输入流和输出流 fileInputStream.close(); out.close(); }
标签:java,ZIP,代码,filename,fileInputStream,bytesRead,response,下载,out From: https://www.cnblogs.com/james-roger/p/17689212.html