cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
package cn.ksource.core.util;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
 
public class DownloadUtil {
 
    /**
     * 下载文件
     * @param response
     * @param file
     * @param fileName
     * 作者:杨凯
     */
    public static void download(HttpServletResponse response,File file,String fileName,Boolean deleteFile) throws IOException{
        response.setCharacterEncoding("gbk");
        // 以流的形式下载文件。
        InputStream fis = new BufferedInputStream(new FileInputStream(file));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        // 清空response
        response.reset();
        // 设置response的Header
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"), "ISO8859-1"));
        response.addHeader("Content-Length", "" + file.length());
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        
        String name = file.getName();
        System.out.println(name );
        String suffix = name.substring(name.lastIndexOf(".")+1, name.length()).toLowerCase();
        if ("doc".equals(suffix)) {
            response.setContentType("application/msword");
        }else if ("xls".equals(suffix)) {
            response.setContentType("application/msexcel");
        }else if ("pdf".equals(suffix)) {
            response.setContentType("application/pdf");
        }else if ("txt".equals(suffix)) {
            response.setContentType("application/txt");
        }else{
            response.setContentType("application/octet-stream");
        }
        
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
        response.flushBuffer();
        if (deleteFile) {
            file.delete();
        }
    }
    
    /**
     * 下载文件
     * @param response
     * @param file
     * @param fileName
     * 作者:杨凯
     */
    public static void download(HttpServletResponse response,InputStream fis,String fileName) throws IOException{
        response.setCharacterEncoding("gbk");
        response.addHeader("Content-Length", "" + fis.available());
        // 以流的形式下载文件。
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        // 清空response
        response.reset();
        // 设置response的Header
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"), "ISO8859-1"));
    
        
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        
        String suffix = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()).toLowerCase();
        if ("doc".equals(suffix)) {
            response.setContentType("application/msword");
        }else if ("xls".equals(suffix)) {
            response.setContentType("application/msexcel");
        }else if ("pdf".equals(suffix)) {
            response.setContentType("application/pdf");
        }else if ("txt".equals(suffix)) {
            response.setContentType("application/txt");
        }else{
            response.setContentType("application/octet-stream");
        }
        
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
        response.flushBuffer();
    }
    
    
    public static String toClientForReport(HttpServletResponse response,ByteArrayOutputStream baos,String name) throws UnsupportedEncodingException{
        response.setHeader("Content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "inline; filename=\""+new String(name.getBytes("GB2312"),"ISO8859-1")+".xls\"");
        response.setCharacterEncoding("UTF-8");
        try {
            ServletOutputStream out = response.getOutputStream();
            baos.writeTo(out);
            baos.flush();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}