1
2
3
4
5 package ecar.servlet.file;
6
7 import java.io.File;
8 import java.io.FileInputStream;
9 import java.io.IOException;
10 import java.io.OutputStream;
11 import java.util.HashMap;
12 import java.util.Iterator;
13
14 import javax.servlet.ServletException;
15 import javax.servlet.http.HttpServlet;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18
19 import org.apache.log4j.Logger;
20
21 import comum.util.Util;
22 import ecar.util.ConfiguracaoResponseBean;
23
24
25
26
27
28 public class DownloadFile extends HttpServlet{
29
30
31
32
33 private static final long serialVersionUID = 5548532554690785199L;
34 private Logger logger = Logger.getLogger(this.getClass());
35
36
37
38
39
40
41 public DownloadFile(){
42
43 }
44
45
46
47
48
49
50
51
52
53
54
55
56 public final void service(HttpServletRequest request,
57 HttpServletResponse response) throws ServletException, IOException {
58 String strDownloadDeslogado = request.getParameter("acessoPublico");
59 if (strDownloadDeslogado != null) {
60 request.getSession().setAttribute("downloadDeslogado",
61 new Boolean(true));
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 String tipo = request.getParameter("tipo");
79 boolean download = true;
80 boolean multimidia = false;
81 if (tipo != null && "open".equals(tipo))
82 download = false;
83 if (tipo != null && "multimidia".equals(tipo)) {
84 multimidia = true;
85 }
86
87 String file = request.getParameter("RemoteFile");
88 file = Util.trocar(file, "/", File.separator);
89 String fileName = file.substring(file.lastIndexOf(File.separator) + 1);
90 String porta = String.valueOf(request.getLocalPort());
91
92 if (multimidia) {
93 response.setContentType("video/mpeg");
94 response.setHeader("Cache-Control", "cache; filename=\"" + fileName
95 + "\"");
96 } else if (download) {
97
98
99
100
101 ConfiguracaoResponseBean configuracaoResponseBean = (ConfiguracaoResponseBean) request
102 .getSession().getServletContext().getAttribute(
103 ConfiguracaoResponseBean.class.getName());
104
105
106 if (configuracaoResponseBean != null
107 && configuracaoResponseBean
108 .getInUtilizarConfiguracaoResponse()) {
109
110
111 if (configuracaoResponseBean.getInSetarContentLength()) {
112 File f = new File(file);
113 response.setContentLength((int) f.length());
114 }
115
116 if (configuracaoResponseBean.getContentType() != null) {
117 response.setContentType(configuracaoResponseBean
118 .getContentType());
119 }
120
121 HashMap<String, String> hmHeaders = configuracaoResponseBean
122 .getHeaders();
123 Iterator iterator = hmHeaders.keySet().iterator();
124
125 while (iterator.hasNext()) {
126 String chave = (String) iterator.next();
127 String valor = (String) hmHeaders.get(chave);
128
129 response.setHeader(chave, valor);
130 }
131 } else {
132
133 File f = new File(file);
134 response.setContentLength((int) f.length());
135 response.setContentType("application/octetstream");
136 response.setHeader("Pragma", "Public");
137 response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
138 }
139 } else {
140 response.setContentType("image/jpeg");
141 response.setHeader("Cache-Control", "no-cache; filename=\""
142 + fileName + "\"");
143 }
144 java.io.InputStream is = null;
145 OutputStream out2 = null;
146 try {
147 out2 = response.getOutputStream();
148 is = new FileInputStream(file);
149 int i;
150 while ((i = is.read()) != -1) {
151 out2.write(i);
152 }
153 } catch (Exception e) {
154 this.logger.error(e);
155 logger.equals(e);
156 } finally {
157 if (out2 != null) {
158 out2.flush();
159 out2.close();
160 }
161 if (is != null) {
162 is.close();
163 }
164 }
165 }
166
167 private void configurarResponse(HttpServletResponse response) {
168
169 }
170 }