P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
package foundation.io.object;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
 
import javax.servlet.http.HttpServletResponse;
 
import foundation.dao.DataWriter;
import foundation.handler.LetterWriter;
import foundation.handler.ResultPool;
import foundation.json.JSONWriter;
 
public class DownloadWriter extends LetterWriter {
 
    private HttpServletResponse response;
    
    public DownloadWriter(DataWriter dataWriter) {
        ResultPool resultPool = dataWriter.getResultPool();
        
        this.response = resultPool.getResponse();
        resultPool.setFireReplay(false);
    }
 
    public void write(File file, String fileName, DownloadAction action) throws IOException {
        writeHeader(fileName, action);
        
        OutputStream outputStream = response.getOutputStream();
        
        try {
            if (file != null && file.exists()) {
                InputStream inputStream = new FileInputStream(file);
                try {
                    byte[] buffer = new byte[1024]; int numberRead = 0;
                    
                    while ((numberRead = inputStream.read(buffer)) != -1) {  
                        outputStream.write(buffer, 0, numberRead);       
                    }  
                }
                finally {
                    inputStream.close();
                }                
            }
            else {
                String message = "foundation/file " + fileName + " not find";
                outputStream.write(message.getBytes());
            }
        }
        finally {
            outputStream.flush();
            outputStream.close();
        }
    }
    
    public void write(String name, StringBuilder content, DownloadAction action) throws IOException {
        writeHeader(name, action);
        
        PrintWriter writer = response.getWriter();
        try {
            writer.write(content.toString());
        }
        finally {
            if (writer != null) {
                writer.flush();
                writer.close();
            }
        }
    }
 
    private void writeHeader(String name, DownloadAction action) throws UnsupportedEncodingException {
        if (action == null) {
            action = DownloadAction.SaveAs;
        }
        
        String prefix = "";
        
        if (DownloadAction.SaveAs == action) {
            response.setHeader("Content-Type", "application/octet-stream");
            prefix = "attachment;";
        }
        else if (DownloadAction.AsJPEG == action) {
            response.setHeader("Content-Type", "image/jpeg");
        }
        else if (DownloadAction.AsPng == action) {
            response.setHeader("Content-Type", "image/png");
        }
        else if (DownloadAction.AsGIF == action) {
            response.setHeader("Content-Type", "image/gif");
        }
        else if (DownloadAction.AsBmp == action) {
            response.setHeader("Content-Type", "image/bmp");
        }
        else if (DownloadAction.AsExcel == action) {
            response.setHeader("Content-Type", "application/vnd.ms-excel");
        }
        else if (DownloadAction.AsWord == action) {
            response.setHeader("Content-Type", "application/msword");
        }        
        else if (DownloadAction.AsPowerPoint == action) {
            response.setHeader("Content-Type", "application/x-ppt");
        }
        else if (DownloadAction.AsPDF == action) {
            response.setHeader("Content-Type", "application/pdf");
        }        
        else if (DownloadAction.ASTexT == action) {
            response.setHeader("Content-Type", "text/plain");
        }        
        
        response.setHeader("Content-Disposition", prefix + "filename=" + JSONWriter.encodeValue(name));
    }
 
}