[Java] 下载zip文件到客户端 →→→→→进入此内容的聊天室

来自 , 2019-03-16, 写在 Java, 查看 139 次.
URL http://www.code666.cn/view/c26820b8
  1. public void doGet(HttpServletRequest request, HttpServletResponse response)
  2.                         throws ServletException, IOException {
  3.                 String path = this.getServletContext().getRealPath(
  4.                                 "/WEB-INF/huoju-client.zip");
  5.  
  6.                 BufferedInputStream bis = null;
  7.                 BufferedOutputStream bos = null;
  8.                 OutputStream fos = null;
  9.                 InputStream fis = null;
  10.                 try {
  11.                         response.setContentType("application/octet-stream");
  12.                         response.setHeader("Content-disposition", "attachment;filename="
  13.                                         + "huoju-client.zip");
  14.                         fis = new FileInputStream(path);
  15.                         bis = new BufferedInputStream(fis);
  16.                         fos = response.getOutputStream();
  17.                         bos = new BufferedOutputStream(fos);
  18.                         int bytesRead = 0;
  19.                         byte[] buffer = new byte[5 * 1024];
  20.                         while ((bytesRead = bis.read(buffer)) != -1) {
  21.                                 bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
  22.                         }
  23.                         bos.close();
  24.                         bis.close();
  25.                         fos.close();
  26.                         fis.close();
  27.                 } catch (IOException e) {
  28.                         response.reset();
  29.                         e.printStackTrace();
  30.                 } finally {
  31.                         try {
  32.                                 if (fos != null) {
  33.                                         fos.close();
  34.                                 }
  35.                                 if (bos != null) {
  36.                                         bos.close();
  37.                                 }
  38.                                 if (fis != null) {
  39.                                         fis.close();
  40.                                 }
  41.                                 if (bis != null) {
  42.                                         bis.close();
  43.                                 }
  44.                         } catch (IOException e) {
  45.                                 System.err.print(e);
  46.                         }
  47.                 }

回复 "下载zip文件到客户端"

这儿你可以回复上面这条便签

captcha