chenjiahe
2023-09-01 2e06d10f39d8bbdeae8a8da99f3d94ed027df24c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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();
        }
    }
 
}