Commit c18ecdb37151992553ce408e72f13af40326311b
1 parent
ef935bb9
Exists in
master
Validação de tipo de arquivos.
Showing
2 changed files
with
47 additions
and
37 deletions
Show diff stats
WebContent/WEB-INF/jsp/planoTrabalho/planoTrabalho.jsp
@@ -33,7 +33,7 @@ | @@ -33,7 +33,7 @@ | ||
33 | <input type="text" id="valorHash" name="valorHash" size="60" maxlength="60" required><br><br> | 33 | <input type="text" id="valorHash" name="valorHash" size="60" maxlength="60" required><br><br> |
34 | <label for="file">Arquivo: </label> | 34 | <label for="file">Arquivo: </label> |
35 | <input type="file" id="file" name="file" accept= | 35 | <input type="file" id="file" name="file" accept= |
36 | -"application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, | 36 | +"application/msword, application/vnd.ms-excel, application/vnd.oasis.opendocument.text , |
37 | text/plain, application/pdf, image/*" required><br><br> | 37 | text/plain, application/pdf, image/*" required><br><br> |
38 | <input type="submit" class="button" value="Enviar"> | 38 | <input type="submit" class="button" value="Enviar"> |
39 | </fieldset> | 39 | </fieldset> |
src/br/com/controller/PlanoTrabalhoController.java
@@ -14,59 +14,69 @@ import br.com.model.entity.Questionario; | @@ -14,59 +14,69 @@ import br.com.model.entity.Questionario; | ||
14 | 14 | ||
15 | @Resource | 15 | @Resource |
16 | public class PlanoTrabalhoController { | 16 | public class PlanoTrabalhoController { |
17 | - | 17 | + |
18 | private Validator validator; | 18 | private Validator validator; |
19 | private Result result; | 19 | private Result result; |
20 | private ServletContext application; | 20 | private ServletContext application; |
21 | private String path; | 21 | private String path; |
22 | - | ||
23 | - public PlanoTrabalhoController(Result result, Validator validator, ServletContext application) | ||
24 | - { | 22 | + |
23 | + public PlanoTrabalhoController(Result result, Validator validator, ServletContext application) { | ||
25 | this.validator = validator; | 24 | this.validator = validator; |
26 | this.application = application; | 25 | this.application = application; |
27 | this.path = this.application.getRealPath("") + "/WEB-INF/conexao.properties"; | 26 | this.path = this.application.getRealPath("") + "/WEB-INF/conexao.properties"; |
28 | this.result = result; | 27 | this.result = result; |
29 | } | 28 | } |
30 | - | 29 | + |
31 | @Path("/planoTrabalho-site") | 30 | @Path("/planoTrabalho-site") |
32 | - public void planoTrabalho() | ||
33 | - { | 31 | + public void planoTrabalho() { |
34 | 32 | ||
35 | } | 33 | } |
36 | - | 34 | + |
37 | @Path("/plano-trabalho-confirmacao") | 35 | @Path("/plano-trabalho-confirmacao") |
38 | - public void planoTrabalhoConfirmacao() | ||
39 | - { | ||
40 | - | 36 | + public void planoTrabalhoConfirmacao() { |
37 | + | ||
41 | } | 38 | } |
42 | - | 39 | + |
43 | @Path("/salvar-plano-trabalho") | 40 | @Path("/salvar-plano-trabalho") |
44 | - public void enviarPlanoTrabalho(String valorHash, UploadedFile file) | ||
45 | - { | ||
46 | - HashDao hashDao = new HashDao(); | ||
47 | - Questionario questionario = new Questionario(); | ||
48 | - questionario = hashDao.verificarHash(path, valorHash); | 41 | + public void enviarPlanoTrabalho(String valorHash, UploadedFile file) { |
42 | + String tipoArquivo = file.getContentType(); | ||
49 | String mensagem = "Não foi possível enviar o plano de trabalho!"; | 43 | String mensagem = "Não foi possível enviar o plano de trabalho!"; |
50 | boolean status = false; | 44 | boolean status = false; |
51 | - | ||
52 | - if(questionario != null && questionario.getDtInicioQuestionario() != null) | ||
53 | - { | ||
54 | - PlanoTrabalhoDao planoTrabalhoDao = new PlanoTrabalhoDao(); | ||
55 | - | ||
56 | - if(planoTrabalhoDao.salvarPlanoTrabalho(valorHash, file.getFile(), questionario)) | ||
57 | - { | ||
58 | - mensagem = "Plano de trabalho enviado com sucesso!"; | ||
59 | - status = true; | ||
60 | - } | ||
61 | - } | ||
62 | - else | ||
63 | - { | ||
64 | - mensagem = "Favor verificar o Código Hash!"; | 45 | + |
46 | + if (!tipoArquivo.equalsIgnoreCase("application/msword") | ||
47 | + && !tipoArquivo.equalsIgnoreCase("application/vnd.ms-excel") | ||
48 | + && !tipoArquivo.equalsIgnoreCase("application/vnd.oasis.opendocument.text") | ||
49 | + && !tipoArquivo.equalsIgnoreCase("text/plain") | ||
50 | + && !tipoArquivo.equalsIgnoreCase("application/pdf") | ||
51 | + && !tipoArquivo.equalsIgnoreCase("image/png") | ||
52 | + && !tipoArquivo.equalsIgnoreCase("image/gif") | ||
53 | + && !tipoArquivo.equalsIgnoreCase("image/jpeg")) { | ||
54 | + | ||
55 | + mensagem = "Formato de Arquivo inválido!"; | ||
56 | + status = false; | ||
57 | + | ||
58 | + } else { | ||
59 | + HashDao hashDao = new HashDao(); | ||
60 | + Questionario questionario = new Questionario(); | ||
61 | + questionario = hashDao.verificarHash(path, valorHash); | ||
62 | + | ||
63 | + | ||
64 | + if (questionario != null && questionario.getDtInicioQuestionario() != null) { | ||
65 | + PlanoTrabalhoDao planoTrabalhoDao = new PlanoTrabalhoDao(); | ||
66 | + | ||
67 | + if (planoTrabalhoDao.salvarPlanoTrabalho(valorHash, file.getFile(), questionario)) { | ||
68 | + mensagem = "Plano de trabalho enviado com sucesso!"; | ||
69 | + status = true; | ||
70 | + } | ||
71 | + | ||
72 | + else { | ||
73 | + mensagem = "Favor verificar o Código Hash!"; | ||
74 | + } | ||
75 | + } | ||
65 | } | 76 | } |
66 | - result.include("mensagem", mensagem); | ||
67 | - result.include("status", status); | ||
68 | - this.validator.add(new ValidationMessage("", "")); | ||
69 | - this.validator.onErrorUsePageOf(PlanoTrabalhoController.class).planoTrabalhoConfirmacao(); | 77 | + result.include("mensagem", mensagem); |
78 | + result.include("status", status); | ||
79 | + this.validator.add(new ValidationMessage("", "")); | ||
80 | + this.validator.onErrorUsePageOf(PlanoTrabalhoController.class).planoTrabalhoConfirmacao(); | ||
70 | } | 81 | } |
71 | - | ||
72 | } | 82 | } |