chenjiahe
2022-01-28 b1097dce55d857ae1ae63798911c1e7027c00de4
提交 | 用户 | age
db7fc9 1 package com.hx.util;
C 2
3 import javax.servlet.http.HttpServletRequest;
4 import javax.servlet.http.HttpServletResponse;
5 import java.io.*;
6 import java.net.URLEncoder;
7
8 public class DownFileUtil {
9
10     /**
11      * 下载文件工具
12      * 
13      * @param path
14      *            完整路径(可用simpleToof工具获取)
15      */
16     public static void DownFile(HttpServletRequest request,HttpServletResponse  response,String path) {
17         final String userAgent = request.getHeader("USER-AGENT");
18         try {
19             // AdminUpload adminUpload = dm.find(AdminUpload.class, fileName);
20             // String str =adminUpload.getPath();
21             String filePath = path;
22             // System.out.println("path"+ServletActionContext.getRequest().getRealPath("/")
23             // + filePath);
24             // File downfile = new
25             // File(ServletActionContext.getRequest().getRealPath("/") +
26             // filePath);
27             File downfile = new File(filePath);
28             String filename = "";
29             if(userAgent.equals("MSIE")){//IE浏览器
30                 filename = URLEncoder.encode(downfile.getName(),"UTF8");
31             }else if(userAgent.equals("Mozilla")){//google,火狐浏览器
32                 filename = new String(downfile.getName().getBytes(), "ISO8859-1");
33             }else{
34                 filename = URLEncoder.encode(downfile.getName(),"UTF8");//其他浏览器
35             }
36             InputStream fis = new BufferedInputStream(new FileInputStream(
37                     downfile));
38             byte[] buffer = new byte[fis.available()];
39             fis.read(buffer);
40             fis.close();
41             response.reset();
42             response.addHeader("Content-Disposition",
43                     "attachment;filename=" + filename);
44             response.addHeader("Content-Length",
45                     "" + downfile.length());
46             OutputStream toClient = new BufferedOutputStream(
47                     response.getOutputStream());
48             response.setContentType(
49                     "application/octet-stream");
50             toClient.write(buffer);
51             toClient.flush();
52             toClient.close();
53         } catch (FileNotFoundException e) {
54             e.printStackTrace();
55         } catch (IOException e) {
56             e.printStackTrace();
57         }
58     }
59     
60     /**
61      * 下载文件工具(提示选择路径)
62      * 
63      * @param path
64      *            完整路径(可用simpleToof工具获取)
65      */
66     public static void DownFileTips(HttpServletRequest request,HttpServletResponse response,String path) {
67         final String userAgent = request.getHeader("USER-AGENT");
68         try {
69             // AdminUpload adminUpload = dm.find(AdminUpload.class, fileName);
70             // String str =adminUpload.getPath();
71             String filePath = path;
72             // System.out.println("path"+ServletActionContext.getRequest().getRealPath("/")
73             // + filePath);
74             // File downfile = new
75             // File(ServletActionContext.getRequest().getRealPath("/") +
76             // filePath);
77             File downfile = new File(filePath);
78
79             String filename = "";
80             if(userAgent.equals("MSIE")){//IE浏览器
81                 filename = URLEncoder.encode(downfile.getName(),"UTF8");
82             }else if(userAgent.equals("Mozilla")){//google,火狐浏览器
83                 filename = new String(downfile.getName().getBytes(), "ISO8859-1");
84             }else{
85                 filename = URLEncoder.encode(downfile.getName(),"UTF8");//其他浏览器
86             }
87             InputStream fis = new BufferedInputStream(new FileInputStream(
88                     downfile));
89             byte[] buffer = new byte[fis.available()];
90             fis.read(buffer);
91             fis.close();
92             
93             response.addHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes()));  
94             OutputStream toClient= new BufferedOutputStream(response.getOutputStream());  
95             response.setContentType("application/vnd.ms-excel;charset=utf-8");  
96                
97             toClient.write(buffer);
98             toClient.flush();
99             toClient.close();
100         } catch (FileNotFoundException e) {
101             e.printStackTrace();
102         } catch (IOException e) {
103             e.printStackTrace();
104         }
105     }
106
107     /**
108      * 下载文件工具(提示选择路径)
109      *
110      * @param downfile
111      *            导出的文件
112      * @param fileName
113      *            导出的文件名称
114      */
115     public static void DownFileTips(HttpServletRequest request, HttpServletResponse response, File downfile, String fileName) {
116         final String userAgent = request.getHeader("USER-AGENT");
117         try {
118             if(StringUtils.isEmpty(fileName)){
119                 fileName = downfile.getName();
120             }
121             String filename =  "";
122             if(userAgent.equals("MSIE")){//IE浏览器
123                 filename = URLEncoder.encode(fileName,"UTF8");
124             }else if(userAgent.equals("Mozilla")){//google,火狐浏览器
125                 filename = new String(fileName.getBytes(), "ISO8859-1");
126             }else{
127                 filename = URLEncoder.encode(fileName,"UTF8");//其他浏览器
128             }
129             InputStream fis = new BufferedInputStream(new FileInputStream(
130                     downfile));
131             byte[] buffer = new byte[fis.available()];
132             fis.read(buffer);
133             fis.close();
134
135             response.addHeader("Content-Disposition", "attachment;filename="+ filename);
136             OutputStream toClient= new BufferedOutputStream(response.getOutputStream());
137             response.setContentType("application/vnd.ms-excel;charset=utf-8");
138
139             toClient.write(buffer);
140             toClient.flush();
141             toClient.close();
142         } catch (FileNotFoundException e) {
143             e.printStackTrace();
144         } catch (IOException e) {
145             e.printStackTrace();
146         }
147     }
148
149 }