JSF, Primefaces, p: fileupload stopped working after chrome update

0

fileupload worked perfectly for up to 3 Chrome updates ago, Firefox never worked, I searched the internet for some solution or similar error and I was not successful.

The page in question should attach the file to then send, it happens that when clicked on the button the window to choose the file does not open.

Upload code:

<div class="botoesAnexo">
  <div class="botoesAnexoLeft">
	  <p:fileUpload fileUploadListener="#{cadastroDocumentoController.handleFileUpload}"
    label="Procurar..." mode="advanced"
    skinSimple="false" update="anexoDocumento"
    dragDropSupport="true"
    auto="true"
    multiple="true"
    allowTypes="/(\.|\/)(pdf|doc|zip|tar|rar|xls|ppt|docx|tif|jpe?g)$/"
    />
	</div>
</div>

CodeCodeDocumentController:

package br.com.sysmar.sysportal.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.commons.io.FilenameUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import br.com.sysmar.sysportal.help.ArquivoAnexoHelp;
import br.com.sysmar.sysportalmodel.interfaceFacade.IConfiguracaoFacade;
import br.com.sysmar.sysportalmodel.interfaceFacade.IDocumentoFacade;
import br.com.sysmar.sysportalmodel.interfaceModel.IArquivoAnexo;
import br.com.sysmar.sysportalmodel.interfaceModel.ICategoria;
import br.com.sysmar.sysportalmodel.interfaceModel.IDocumento;
import br.com.sysmar.sysportalmodel.interfaceModel.IEntidade;
import br.com.sysmar.sysportalmodel.interfaceModel.IFiltro;
import br.com.sysmar.sysportalmodel.interfaceModel.IOpcaoFiltro;
import sysmarutils.DateUtils;
import sysmarutils.exceptions.WarningException;

@ManagedBean(name = "cadastroDocumentoController")
@Controller
@Scope("view")
public class CadastroDocumentoController {
	
	@Autowired
	private IDocumentoFacade documentoFacade;
	
	@Autowired
	private IConfiguracaoFacade configuracaoFacade;
	
	private Long categoriaId;
	
	private Long documentoId;
	
	private ICategoria categoria;
	
	private IArquivoAnexo anexo;
	
	private ArquivoAnexoHelp anexoHelp;
	
	private IEntidade entidade;
	
	private String tamanhoArquivo;
	
	private IDocumento documento;
	
	private Date dataDocumento;
	
	private StreamedContent file;
	
	private ArrayList<ArquivoAnexoHelp> listaAnexo = new ArrayList<ArquivoAnexoHelp>();
	
	private ArrayList<IFiltro> filtroList = new ArrayList<IFiltro>();
	
	private IOpcaoFiltro[] opcaoFiltroSelecionadaList = new IOpcaoFiltro[10];
	
	public boolean init;
	
	public boolean apresentaInformativo;
	
	FacesContext context;
	
	@Transactional
	public void inicializar() throws Exception {
		if(init == false) {
			entidade = configuracaoFacade.carregarEntidade();

			if(categoriaId != null) 
				initCategoria();
			else if(documentoId != null)
				initDocumento();
			else
				documento = documentoFacade.novoDocumento();
			
			init = true;
		}
	}

	private void initDocumento() {
		documento = documentoFacade.recuperarDocumento(documentoId);
		categoria = documento.getCategoria();
		listaAnexo = new ArrayList<ArquivoAnexoHelp>();
		preencherDatas();
		preencherFiltroList(documento.getCategoria());
		documento.getOpcaoFiltroList();

		carregarOpcaoFiltroSelecionadas(documento);
		
		ICategoria categoriaSelecionada = documento.getCategoria();
		
		if(documento.getArquivoAnexoList() != null)
			recuperarAnexos(categoriaSelecionada);
		
		if(!documento.getInformativo().isEmpty()) 
			apresentaInformativo = true;
	}
	
	private void carregarOpcaoFiltroSelecionadas(IDocumento documento) {
		int i = 0;
		Iterator<IOpcaoFiltro> opcaoFiltroList = documento.getOpcaoFiltroList();
		while (opcaoFiltroList.hasNext()) {
			opcaoFiltroSelecionadaList[i] = opcaoFiltroList.next();
			i++;
		}
	}

	private void preencherFiltroList(ICategoria categoria) {
		Iterator<IFiltro> categoriaIterator = categoria.getFiltroList();
		while (categoriaIterator.hasNext()) {
			IFiltro filtro = categoriaIterator.next();
			filtro.getOpcaoFiltroList();
			filtroList.add(filtro);
		}
	}

	private void initCategoria() {
		categoria = documentoFacade.recuperarCategoria(categoriaId);
		listaAnexo = new ArrayList<ArquivoAnexoHelp>();
		preencherFiltroList(categoria);
		
		documento = documentoFacade.novoDocumento();
		documento.setCategoria(categoria);
		documento.setAtivo(true);
		
		preencherDatas();
	}

	@Transactional
	public String gravar() throws WarningException {
		try {
			if(apresentaInformativo && documento.getInformativo().isEmpty()) {
				FacesContext.getCurrentInstance().addMessage("cadastro:messages", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Informativo do documento não informado", null));
				return "erro";
				
			} else if(!apresentaInformativo) {
				documento.setInformativo("");
			}
			
			documento.setDataDocumento(DateUtils.getCalendar(getDataDocumento()));
			autualizarOpcaoFiltro();
			documentoFacade.gravarDocumento(documento);
			FacesContext.getCurrentInstance().addMessage("cadastro:messages", new FacesMessage("Cadastro Realizado com sucesso"));
			
			FacesContext.getCurrentInstance().getExternalContext().redirect("listCategorias.xhtml?mensagemSucesso=true");
			
			init = false;
			
			categoriaId = null;
			documentoId = null;
			
			listaAnexo = new ArrayList<ArquivoAnexoHelp>();
			
			return "sucesso";
			
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage("cadastro:messages", new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage().toString(), null));
			return "erro";		
		}
		
	}

	private void autualizarOpcaoFiltro() {
		Iterator<IFiltro> filtroList = categoria.getFiltroList();
		
		while (filtroList.hasNext()) {
			IFiltro filtro = filtroList.next();
			IOpcaoFiltro opcaoSelecionada = getOpcaoSelecionada(filtro);
			excluirOpcaoFiltro(filtro);
			if (opcaoSelecionada != null)
				documento.incluirOpcaoFiltro(opcaoSelecionada);
		}
	}

	private void excluirOpcaoFiltro(IFiltro filtro) {
		Iterator<IOpcaoFiltro> opcaoFiltroList = documento.getOpcaoFiltroList();
		while(opcaoFiltroList.hasNext()) {
			IOpcaoFiltro opcaoFiltro = opcaoFiltroList.next();
			if (opcaoFiltro.getFiltro().equals(filtro)) {
				documento.excluirOpcaoFiltro(opcaoFiltro);
				return;
			}
		}
	}
	
	public Integer getIndex(IFiltro filtro) {
		if (filtro == null) return 10;
		
		for (int i = 0; i < opcaoFiltroSelecionadaList.length; i++) {
			if(opcaoFiltroSelecionadaList[i] != null && filtro.equals(opcaoFiltroSelecionadaList[i].getFiltro()))
				return i;
		}
		
		return getProximoIndexVazio();
	}

	private Integer getProximoIndexVazio() {
		for (int i = 0; i < opcaoFiltroSelecionadaList.length; i++) {
			if (opcaoFiltroSelecionadaList[i] == null)
				return i;
		}
		
		return null;
	}
	
	private IOpcaoFiltro getOpcaoSelecionada(IFiltro filtro) {
		for (IOpcaoFiltro opcaoFiltro : opcaoFiltroSelecionadaList) {
			if (opcaoFiltro != null && opcaoFiltro.getFiltro().equals(filtro))
				return opcaoFiltro;
		}

		return null;
	}

	@Transactional
	public String removerDocumento() throws WarningException {
		try {
			if(documento != null) {
				
				IDocumento documentoSelecionado;
				documentoSelecionado = documentoFacade.recuperarDocumento(documento.getId());
				
				documentoFacade.excluirDocumento(documentoSelecionado);
				FacesContext.getCurrentInstance().addMessage("cadastro:messages", new FacesMessage("Remoção Realizada com sucesso"));
				
				FacesContext.getCurrentInstance().getExternalContext().redirect("listCategorias.xhtml?mensagemRemocaoSucesso=true");
				return "sucesso";
			}
			
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage("cadastro:messages", new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage().toString(), null));
			return "erro";		
		}
		
		return null;
		
	}
	
	private void recuperarAnexos(ICategoria categoriaSelecionada) {
		Iterator<IArquivoAnexo> anexosDocumento = documento.getArquivoAnexoList();
		while (anexosDocumento.hasNext()) {
			IArquivoAnexo anexo = anexosDocumento.next();
			ArquivoAnexoHelp anexoHelp = new ArquivoAnexoHelp();
			
			anexoHelp.setId(anexo.getId());
			anexoHelp.setNomeArquivo(anexo.getNomeArquivo());
			anexoHelp.setDataAnexo(anexo.getDataAnexo());
			
			Long tamanho = anexo.getTamanho();
			String tamanhoCalculado = calculaTamanhoArquivo(tamanho);
			anexoHelp.setTamanho(tamanhoCalculado);
			
			String caminho = anexo.getNomeArquivoDiretorio(categoriaSelecionada.getDiretorioAnexos(entidade));
			anexoHelp.setCaminhoAnexo(caminho);
			
			File arquivo = new File(anexoHelp.getCaminhoAnexo());
			
			if(arquivo.exists()) {
				anexoHelp.setArquivoNovo(false);
				anexoHelp.setArquivoExistente(true);
				
				String extencao = FilenameUtils.getExtension(anexo.getNomeArquivo());
				anexoHelp.setExtensaoArquivo(extencao.toLowerCase());

			} else {
				anexoHelp.setArquivoNovo(true);
				anexoHelp.setArquivoExistente(false);					
			}
			
			listaAnexo.add(anexoHelp);						
		}
	}
	
	public void handleFileUpload(FileUploadEvent event) throws IOException {
		UploadedFile file = event.getFile();
		
		ArquivoAnexoHelp anexoHelp = new ArquivoAnexoHelp();
		InputStream stream = event.getFile().getInputstream();
		
		anexo = documentoFacade.novoArquivoAnexo();
		anexo.setNomeArquivo(file.getFileName());
		anexoHelp.setNomeArquivo(file.getFileName());
		
		anexo.setDataAnexo(Calendar.getInstance());
		anexoHelp.setDataAnexo(Calendar.getInstance());
		
		anexo.setInputStreamFile(stream);
		anexo.setTamanho(file.getSize());
		
		String tamanho = calculaTamanhoArquivo(file.getSize());	
		setTamanhoArquivo(tamanhoArquivo);
		anexoHelp.setTamanho(tamanho);
		
		String caminho = documento.getCategoria().getDiretorioAnexos(entidade);
		anexoHelp.setCaminhoAnexo(caminho);
		anexoHelp.setArquivoNovo(true);
		
		documento.incluirAnexo(anexo);
		
		listaAnexo.add(anexoHelp);
		
		
    }
	
	private String calculaTamanhoArquivo(long tamanho) {
		long tamanho_kilobytes = tamanho / 1024 ;
		
		long tamanho_mb = tamanho_kilobytes / 1024;
		
		if(tamanho_mb > 0) {
			tamanhoArquivo = tamanho_mb + " MB ";
		} else {
			tamanhoArquivo = tamanho_kilobytes + " KB ";
		}
		return tamanhoArquivo;
	}
	
	public void prepararArquivo(ArquivoAnexoHelp anexoHelp) {
		if(anexoHelp != null) {
			this.anexoHelp = anexoHelp;
		}
	}
	
	public StreamedContent getFile() {
		try {
			ArquivoAnexoHelp arquivoAnexo = anexoHelp;
			String caminho = arquivoAnexo.getCaminhoAnexo();
			
			InputStream stream = new FileInputStream(caminho);
			file = new DefaultStreamedContent(stream, "application/pdf", arquivoAnexo.getNomeArquivo());
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage("cadastro:messages", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Arquivo não encontrado!", null));
		}
		
		return file;
	}
	
	public void excluirAnexo() {
		if(anexoHelp != null) {
			IArquivoAnexo arquivoAnexo = configuracaoFacade.recuperarAnexo(anexoHelp.getId());
			documento.excluirAnexo(arquivoAnexo);
			
			listaAnexo.remove(anexoHelp);
		}
		
		getListaAnexo();
	}
	
	private void preencherDatas() {
		if(documento.getDataDocumento() != null) {
			dataDocumento = DateUtils.getDate(documento.getDataDocumento());
		}		
	}
	
	public List<SelectItem> getOpcaoSelectItemList(IFiltro filtro) {
		Iterator<IOpcaoFiltro> iOpcaoFiltroList = filtro.getOpcaoFiltroList();
		List<SelectItem> selectItemOpcao = new ArrayList<SelectItem>();
		
		selectItemOpcao.add(new SelectItem(null, "Selecione um Item"));
		while (iOpcaoFiltroList.hasNext()) {
			IOpcaoFiltro opcaoFiltro = iOpcaoFiltroList.next();
			selectItemOpcao.add(new SelectItem(opcaoFiltro, opcaoFiltro.getDescricao()));
		}
		
		return selectItemOpcao;
	}
	
	public List<IFiltro> getFiltroList() {
		return filtroList;
	}
	
	public boolean isRenderizaOpcoesFiltro() {
		return categoria.getFiltroList().hasNext();
	}

	public IDocumentoFacade getDocumentoFacade() {
		return documentoFacade;
	}

	public void setDocumentoFacade(IDocumentoFacade documentoFacade) {
		this.documentoFacade = documentoFacade;
	}

	public Long getCategoriaId() {
		return categoriaId;
	}

	public void setCategoriaId(Long categoriaId) {
		this.categoriaId = categoriaId;
	}

	public ICategoria getCategoria() {
		return categoria;
	}

	public void setCategoria(ICategoria categoria) {
		this.categoria = categoria;
	}

	public IDocumento getDocumento() {
		return documento;
	}

	public void setDocumento(IDocumento documento) {
		this.documento = documento;
	}

	public Date getDataDocumento() {
		return dataDocumento;
	}

	public void setDataDocumento(Date dataDocumento) {
		this.dataDocumento = dataDocumento;
	}

	public IConfiguracaoFacade getConfiguracaoFacade() {
		return configuracaoFacade;
	}

	public void setConfiguracaoFacade(IConfiguracaoFacade configuracaoFacade) {
		this.configuracaoFacade = configuracaoFacade;
	}

	public IArquivoAnexo getAnexo() {
		return anexo;
	}

	public void setAnexo(IArquivoAnexo anexo) {
		this.anexo = anexo;
	}

	public String getTamanhoArquivo() {
		return tamanhoArquivo;
	}

	public void setTamanhoArquivo(String tamanhoArquivo) {
		this.tamanhoArquivo = tamanhoArquivo;
	}
	
	public ArrayList<ArquivoAnexoHelp> getListaAnexo() {
		return listaAnexo;
	}
	
	public Long getDocumentoId() {
		return documentoId;
	}

	public void setDocumentoId(Long documentoId) {
		this.documentoId = documentoId;
	}

	public boolean isApresentaInformativo() {
		return apresentaInformativo;
	}

	public void setApresentaInformativo(boolean apresentaInformativo) {
		this.apresentaInformativo = apresentaInformativo;
	}

	public IOpcaoFiltro[] getOpcaoFiltroSelecionadaList() {
		return opcaoFiltroSelecionadaList;
	}

	public void setOpcaoFiltroSelecionadaList(IOpcaoFiltro[] opcaoFiltroSelecionadaList) {
		this.opcaoFiltroSelecionadaList = opcaoFiltroSelecionadaList;
	}
	
}

Web.xml code

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SysPortal</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext-security.xml
         /WEB-INF/applicationContext.xml   
        </param-value>
  </context-param>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet>
  <servlet-name>fileServlet</servlet-name>
  <servlet-class>br.com.sysmar.sysportal.security.FileServlet</servlet-class>
 </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
   <servlet-mapping>
  <servlet-name>fileServlet</servlet-name>
  <url-pattern>/usuario/file/*</url-pattern>
 	</servlet-mapping>
   <servlet-mapping>
  <servlet-name>fileServlet</servlet-name>
  <url-pattern>/master/file/*</url-pattern>
 	</servlet-mapping>
   <servlet-mapping>
  <servlet-name>fileServlet</servlet-name>
  <url-pattern>/gerenciador/file/*</url-pattern>
 	</servlet-mapping>
   <servlet-mapping>
  <servlet-name>fileServlet</servlet-name>
  <url-pattern>/administrador/file/*</url-pattern>
 	</servlet-mapping>
   <servlet-mapping>
  <servlet-name>fileServlet</servlet-name>
  <url-pattern>/public/file/*</url-pattern>
 	</servlet-mapping>
   <servlet-mapping>
  <servlet-name>fileServlet</servlet-name>
  <url-pattern>/file/*</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
  </filter-mapping>
	  <filter>
	    <filter-name>UrlRewriteFilter</filter-name>
	    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
	</filter>
	<filter-mapping>
	    <filter-name>UrlRewriteFilter</filter-name>
	    <url-pattern>/*</url-pattern>
	    <dispatcher>REQUEST</dispatcher>
	    <dispatcher>FORWARD</dispatcher>
	</filter-mapping>
  <filter>
     <filter-name>SetCharacterEncodingFilter</filter-name>
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
     <init-param>
         <param-name>encoding</param-name>
         <param-value>UTF8</param-value>
     </init-param>
     <init-param>
         <param-name>forceEncoding</param-name>
         <param-value>true</param-value>
     </init-param>
  </filter>
  <filter-mapping>
     <filter-name>SetCharacterEncodingFilter</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>
  <context-param>
    <param-name>primefaces.UPLOADER</param-name>
    <param-value>commons</param-value>
  </context-param>
</web-app>

Can anyone tell me what's going on? Thank you in advance, because I really could not find answers anywhere.

    
asked by anonymous 12.11.2018 / 14:09

0 answers