浏览器gzip下载

  1. public static void downloadFile(String filepath, String filename, HttpServletResponse response, PageContext pageContext) {   
  2.   
  3.     InputStream inputStream= null;   
  4.     OutputStream outputStream = null;   
  5.     JspWriter out = pageContext.getOut();   
  6.   
  7.     try {   
  8.         inputStream = new BufferedInputStream(new FileInputStream(filepath));   
  9.   
  10.         String contentType = "application/octet-stream";   
  11. //          contentType = "application/x-gzip";   
  12.   
  13.         response.setContentType(contentType);   
  14. //          response.setHeader("Content-Encoding", "gzip");   
  15.   
  16.         filename = StringUtils.nullToUnknown(filename);   
  17.         filename = WebUtils.convertCharset(filename, Constants.ENCODING_GBK, Constants.ENCODING_ISO88591);   
  18.         response.setHeader("Content-Disposition""attachment;filename=\"" + filename + "\"");   
  19.   
  20.         byte[] bytes = new byte[10240];   
  21.         int i =0;   
  22.         outputStream = response.getOutputStream();   
  23.   
  24.         while ((i = inputStream.read(bytes)) > 0) {   
  25.             outputStream.write(bytes, 0, i);   
  26.         }   
  27.     } catch (Exception e) {   
  28.         //log   
  29.     } finally {   
  30.         WebUtils.closeOutputStream(outputStream);   
  31.         WebUtils.closeInputStream(inputStream);   
  32.         WebUtils.flushResponse(response);   
  33.         WebUtils.outClear(out, pageContext);   
  34.     }   
  35. }  
  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓