fwq
2022-06-17 32e62c64b2e384d11443cb4ae90177d5664dc8d8
提交 | 用户 | 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
db1b40 149     /**
C 150      * 下载文件工具(提示选择路径)
151      *
152      * @param inputStream
153      *            导出的文件流
154      * @param fileName
155      *            导出的文件名称
156      */
157     public static void DownFileTips(HttpServletRequest request, HttpServletResponse response, InputStream  inputStream , String fileName) {
158         final String userAgent = request.getHeader("USER-AGENT");
159         try {
160             if(StringUtils.isEmpty(fileName)){
161                 throw new RuntimeException("请输入文件名称");
162             }
163             String filename =  "";
164             if(userAgent.equals("MSIE")){//IE浏览器
165                 filename = URLEncoder.encode(fileName,"UTF8");
166             }else if(userAgent.equals("Mozilla")){//google,火狐浏览器
167                 filename = new String(fileName.getBytes(), "ISO8859-1");
168             }else{
169                 filename = URLEncoder.encode(fileName,"UTF8");//其他浏览器
170             }
171             InputStream fis = new BufferedInputStream(inputStream);
172             byte[] buffer = new byte[fis.available()];
173             fis.read(buffer);
174             fis.close();
175
176             response.addHeader("Content-Disposition", "attachment;filename="+ filename);
177             OutputStream toClient= new BufferedOutputStream(response.getOutputStream());
178             response.setContentType("application/vnd.ms-excel;charset=utf-8");
179
180             toClient.write(buffer);
181             toClient.flush();
182             toClient.close();
183         } catch (FileNotFoundException e) {
184             e.printStackTrace();
185         } catch (IOException e) {
186             e.printStackTrace();
187         }
188     }
189
db7fc9 190 }