Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 espaco 1
package br.edu.cesmac.sic.core.domain.util;
2
 
3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.File;
6
import java.io.FileInputStream;
7
import java.io.InputStream;
8
import java.io.Serializable;
9
import java.util.Scanner;
10
 
11
import javax.faces.context.FacesContext;
12
import javax.servlet.ServletOutputStream;
13
import javax.servlet.http.HttpServletResponse;
14
 
15
public class AbrirArquivo implements Serializable {
16
 
17
        private static final long serialVersionUID = 1L;
18
        private String nomeArquivo;
19
        private String descricao;
20
 
21
        /**
22
         * Abre o arquivo no browse, passando o nome do arquivo como parametro
23
         *
24
         * @param nomeArquivo
25
         * @throws Exception
26
         */
27
        public void abrir(String nomeArquivo, String descricao) throws Exception {
28
                this.nomeArquivo = nomeArquivo;
29
                this.descricao = descricao;
30
                abrir();
31
        }
32
 
33
        public void abrir() throws Exception {
34
                if (nomeArquivo == null) {
35
                        throw new Exception("Nome do arquivo não foi especificado.");
36
                }
37
 
38
                byte[] arquivo = null;
39
                File file = new File(nomeArquivo);
40
                try {
41
                        arquivo = fileToByte(file);
42
                } catch (Exception e) {
43
                        throw new Exception("Nome do arquivo não foi especificado.\n"
44
                                        + e.getMessage());
45
                }
46
 
47
                HttpServletResponse res = getServletResponse();
48
 
49
                if (nomeArquivo.toLowerCase().endsWith(".doc")
50
                                || nomeArquivo.toLowerCase().endsWith(".docx")) {
51
                        res.setContentType("application/msword");
52
                } else if (nomeArquivo.toLowerCase().endsWith(".txt")) {
53
                        res.setContentType("text/plain");
54
                } else if (nomeArquivo.toLowerCase().endsWith(".pdf")) {
55
                        res.setContentType("application/pdf");
56
                } else if (nomeArquivo.toLowerCase().endsWith(".xls")
57
                                || nomeArquivo.toLowerCase().endsWith(".xlsx")) {
58
                        res.setContentType("application/vnd.ms-excel");
59
                } else if (nomeArquivo.toLowerCase().endsWith(".jpeg")
60
                                || nomeArquivo.toLowerCase().endsWith(".jpg")) {
61
                        res.setContentType("image/jpeg");
62
                } else {
63
                        res.setContentType("application/octet-stream");
64
                }
65
 
66
                res.addHeader("Content-Disposition", "attachment; filename="
67
                                + descricao);
68
 
69
                if (arquivo != null && arquivo.length > 0) {
70
                        res.setContentLength(arquivo.length);
71
                        ServletOutputStream servletOutputStream = res.getOutputStream();
72
                        getFacesContext().responseComplete();
73
 
74
                        servletOutputStream.write(arquivo);
75
                        servletOutputStream.flush();
76
                        servletOutputStream.close();
77
                }
78
        }
79
 
80
        @SuppressWarnings("resource")
81
        public String retornarConteudoDoArquivo(String arquivo) {
82
                InputStream input = getClass().getResourceAsStream(arquivo);
83
                Scanner scanner = new Scanner(input);
84
            scanner.useDelimiter("\\Z");
85
            return scanner.next();
86
        }
87
 
88
        public static InputStream byteToInputStream(byte[] bytes) throws Exception {
89
                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
90
                return bais;
91
        }
92
 
93
        @SuppressWarnings("resource")
94
        public static byte[] fileToByte(File imagem) throws Exception {
95
                FileInputStream fis = new FileInputStream(imagem);
96
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
97
                byte[] buffer = new byte[8192];
98
                int bytesRead = 0;
99
                while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
100
                        baos.write(buffer, 0, bytesRead);
101
                }
102
                return baos.toByteArray();
103
        }
104
 
105
        private static HttpServletResponse getServletResponse() {
106
                return (HttpServletResponse) getFacesContext().getExternalContext()
107
                                .getResponse();
108
        }
109
 
110
        private static FacesContext getFacesContext() {
111
                return FacesContext.getCurrentInstance();
112
        }
113
 
114
        public String getNomeArquivo() {
115
                return nomeArquivo;
116
        }
117
 
118
        public void setNomeArquivo(String nomeArquivo) {
119
                this.nomeArquivo = nomeArquivo;
120
        }
121
 
122
        public void imprimir() throws Exception {
123
                if (nomeArquivo == null) {
124
                        throw new Exception("Nome do arquivo não foi especificado.");
125
                }
126
 
127
                byte[] arquivo = null;
128
                File file = new File(nomeArquivo);
129
                try {
130
                        arquivo = fileToByte(file);
131
                } catch (Exception e) {
132
                        throw new Exception("Nome do arquivo não foi especificado.\n"
133
                                        + e.getMessage());
134
                }
135
 
136
                HttpServletResponse res = getServletResponse();
137
 
138
                if (nomeArquivo.toLowerCase().endsWith(".doc")
139
                                || nomeArquivo.toLowerCase().endsWith(".docx")) {
140
                        res.setContentType("application/msword");
141
                } else if (nomeArquivo.toLowerCase().endsWith(".txt")) {
142
                        res.setContentType("text/plain");
143
                } else if (nomeArquivo.toLowerCase().endsWith(".pdf")) {
144
                        res.setContentType("application/pdf");
145
                } else if (nomeArquivo.toLowerCase().endsWith(".xls")
146
                                || nomeArquivo.toLowerCase().endsWith(".xlsx")) {
147
                        res.setContentType("application/vnd.ms-excel");
148
                } else if (nomeArquivo.toLowerCase().endsWith(".jpeg")
149
                                || nomeArquivo.toLowerCase().endsWith(".jpg")) {
150
                        res.setContentType("image/jpeg");
151
                } else {
152
                        res.setContentType("application/octet-stream");
153
                }
154
 
155
                res.addHeader("Content-Disposition", "attachment; filename="
156
                                + descricao);
157
 
158
                if (arquivo != null && arquivo.length > 0) {
159
                        res.setContentLength(arquivo.length);
160
                        ServletOutputStream servletOutputStream = res.getOutputStream();
161
                        getFacesContext().responseComplete();
162
 
163
                        servletOutputStream.write(arquivo);
164
                        servletOutputStream.flush();
165
                        servletOutputStream.close();
166
                }
167
        }
168
 
169
}