• Spring mvc 字节流


     1     public static void responseDownloadFile(HttpServletRequest request, HttpServletResponse response, File file) throws Exception {
     2         if(file != null && file.length() > 0){
     3             response.setContentType("x-download;charset=UTF-8");              // 显示文件类型      Microsoft Excel 逗号分隔值文件
     4 //            response.setContentType("text/html;charset=UTF-8");              // 显示文件类型     360 Chrome HTML Document
     5 //            response.setContentType("application/csv;charset=UTF-8");       // 显示文件类型      Microsoft Excel 逗号分隔值文件
     6             
     7             request.setCharacterEncoding("UTF-8"); 
     8             response.addHeader("Content-disposition", "attachment; filename="+ new String(file.getName().getBytes(), "ISO8859-1"));     // 解决文件名乱码问题,及文件名中的中文被自动去除问题
     9             response.addHeader("Content-Length", String.valueOf(file.length()));  
    10             
    11             BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));  
    12             BufferedOutputStream bos =  new BufferedOutputStream(response.getOutputStream());  
    13   
    14             byte[] buff = new byte[4096];  
    15             int bytesRead;  
    16             while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
    17                 bos.write(buff, 0, bytesRead);  
    18             }  
    19             
    20             bis.close();  
    21             bos.close(); 
    22         }
  • 相关阅读:
    为什么富人越来越富,穷人越来越穷?
    计算几何基础_点_向量_极角排序
    滑窗模板_双向队列
    后缀数组
    AC自动机
    RMQ_ST表
    二叉树求逆序对(伪AC 23333)
    分块
    莫队
    树状数组_二维
  • 原文地址:https://www.cnblogs.com/kevin443/p/7364744.html
Copyright © 2020-2023  润新知