java下载zip_从Servlet Java下载zip

我不明白为什么这么难,每个人都有自己的实现…

因此,在服务器中,我生成了一个.zip文件,希望用户单击该文件即可下载.

因此,我设置了服务器成功接收到的请求,现在,我正在努力将字节数组写入输出.

这是我的响应代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

System.out.println("Downloading clusters.zip");

/* Generate the directory on the server, then zip it. */

clustersToFiles();

zipClusters();

/* Now the zip is saved on zipFullPath */

System.out.println("Done generating the .zip");

String parent_dir = System.getProperty("catalina.base");

String filename = "clusters.zip";

String zipFullPath = parent_dir + "/" + filename;

response.setContentType("application/zip");

response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

OutputStream out = response.getOutputStream();

FileInputStream fis = new FileInputStream(zipFullPath);

int bytes;

while ((bytes = fis.read()) != -1) {

System.out.println(bytes);

out.write(bytes);

}

fis.close();

response.flushBuffer();

System.out.println(".zip file downloaded at client successfully");

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值