package com.hx.util; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; public class DownFileUtil { /** * 下载文件工具 * * @param path * 完整路径(可用simpleToof工具获取) */ public static void DownFile(HttpServletRequest request,HttpServletResponse response,String path) { final String userAgent = request.getHeader("USER-AGENT"); try { // AdminUpload adminUpload = dm.find(AdminUpload.class, fileName); // String str =adminUpload.getPath(); String filePath = path; // System.out.println("path"+ServletActionContext.getRequest().getRealPath("/") // + filePath); // File downfile = new // File(ServletActionContext.getRequest().getRealPath("/") + // filePath); File downfile = new File(filePath); String filename = ""; if(userAgent.equals("MSIE")){//IE浏览器 filename = URLEncoder.encode(downfile.getName(),"UTF8"); }else if(userAgent.equals("Mozilla")){//google,火狐浏览器 filename = new String(downfile.getName().getBytes(), "ISO8859-1"); }else{ filename = URLEncoder.encode(downfile.getName(),"UTF8");//其他浏览器 } InputStream fis = new BufferedInputStream(new FileInputStream( downfile)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.reset(); response.addHeader("Content-Disposition", "attachment;filename=" + filename); response.addHeader("Content-Length", "" + downfile.length()); OutputStream toClient = new BufferedOutputStream( response.getOutputStream()); response.setContentType( "application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 下载文件工具(提示选择路径) * * @param path * 完整路径(可用simpleToof工具获取) */ public static void DownFileTips(HttpServletRequest request,HttpServletResponse response,String path) { final String userAgent = request.getHeader("USER-AGENT"); try { // AdminUpload adminUpload = dm.find(AdminUpload.class, fileName); // String str =adminUpload.getPath(); String filePath = path; // System.out.println("path"+ServletActionContext.getRequest().getRealPath("/") // + filePath); // File downfile = new // File(ServletActionContext.getRequest().getRealPath("/") + // filePath); File downfile = new File(filePath); String filename = ""; if(userAgent.equals("MSIE")){//IE浏览器 filename = URLEncoder.encode(downfile.getName(),"UTF8"); }else if(userAgent.equals("Mozilla")){//google,火狐浏览器 filename = new String(downfile.getName().getBytes(), "ISO8859-1"); }else{ filename = URLEncoder.encode(downfile.getName(),"UTF8");//其他浏览器 } InputStream fis = new BufferedInputStream(new FileInputStream( downfile)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.addHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes())); 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(); } } /** * 下载文件工具(提示选择路径) * * @param downfile * 导出的文件 * @param fileName * 导出的文件名称 */ public static void DownFileTips(HttpServletRequest request, HttpServletResponse response, File downfile, String fileName) { final String userAgent = request.getHeader("USER-AGENT"); try { if(StringUtils.isEmpty(fileName)){ fileName = downfile.getName(); } 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(new FileInputStream( downfile)); 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(); } } /** * 下载文件工具(提示选择路径) * * @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(); } } /** * 下载文件工具(提示选择路径) * * @param downfile * 导出的文件 * @param fileName * 导出的文件名称 */ public static void DownFileTips( HttpServletResponse response, File downfile, String fileName) { try { if(StringUtils.isEmpty(fileName)){ fileName = downfile.getName(); } String filename = ""; filename = URLEncoder.encode(fileName,"UTF8");//其他浏览器 InputStream fis = new BufferedInputStream(new FileInputStream( downfile)); 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(); } } }