ChenJiaHe
2022-03-05 db1b408d60e2f4d0c48bc875a09ab4c8a2975367
新增文件导出工具方法-流模式
1个文件已修改
41 ■■■■■ 已修改文件
src/main/java/com/hx/util/DownFileUtil.java 41 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/util/DownFileUtil.java
@@ -146,4 +146,45 @@
        }
    }
    /**
     * 下载文件工具(提示选择路径)
     *
     * @param inputStream
     *            导出的文件流
     * @param fileName
     *            导出的文件名称
     */
    public static void DownFileTips(HttpServletRequest request, HttpServletResponse response, InputStream  inputStream , String fileName) {
        final String userAgent = request.getHeader("USER-AGENT");
        try {
            if(StringUtils.isEmpty(fileName)){
                throw new RuntimeException("请输入文件名称");
            }
            String filename =  "";
            if(userAgent.equals("MSIE")){//IE浏览器
                filename = URLEncoder.encode(fileName,"UTF8");
            }else if(userAgent.equals("Mozilla")){//google,火狐浏览器
                filename = new String(fileName.getBytes(), "ISO8859-1");
            }else{
                filename = URLEncoder.encode(fileName,"UTF8");//其他浏览器
            }
            InputStream fis = new BufferedInputStream(inputStream);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.addHeader("Content-Disposition", "attachment;filename="+ filename);
            OutputStream toClient= new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}