Fórum FileUpload não funciona #508438
25/01/2015
0
Olá pessoal,
Estou tento algumas dificuldades em fazer fileupload funcionar. Aparentemente, fiz todas as configurações possíveis, mas nenhuma ação é executada quando clico no componente. Nada acontece, nem uma mensagem de erro aparece. O que estou fazendo de errado? Segue abaixo os códigos:
Classe Bean
XHTML
Web.xml
pom.xml
Obrigada.
Estou tento algumas dificuldades em fazer fileupload funcionar. Aparentemente, fiz todas as configurações possíveis, mas nenhuma ação é executada quando clico no componente. Nada acontece, nem uma mensagem de erro aparece. O que estou fazendo de errado? Segue abaixo os códigos:
Classe Bean
import java.io.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;
@Named(value = "arquivosClienteBean")
@SessionScoped
public class ArquivosClienteBean implements Serializable {
private static final long serialVersionUID = 342541594270464604L;
private ArquivosCliente arquivosCliente = new ArquivosCliente();
private List<ArquivosCliente> lista;
private String caminho = "C:\\tmp\\";
private UploadedFile arquivo;
public UploadedFile getArquivo() {
return arquivo;
}
public void setArquivo(UploadedFile arquivo) {
this.arquivo = arquivo;
}
public ArquivosClienteBean() {
}
public void TransferirArquivo(String fileName, InputStream in) {
try {
OutputStream out = new FileOutputStream(new File(caminho + fileName));
int reader = 0;
byte[] bytes = new byte[(int)getArquivo().getSize()];
while ((reader = in.read(bytes)) != -1) {
out.write(bytes, 0, reader);
}
in.close();
out.flush();
out.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public void upload() {
String extValidate;
if (getArquivo() != null) {
String ext = getArquivo().getFileName();
if (ext != null) {
extValidate = ext.substring(ext.indexOf(".")+1);
} else {
extValidate = null;
}
if (extValidate.equals("pdf")) {
try {
TransferirArquivo(getArquivo().getFileName(), getArquivo().getInputstream());
} catch (IOException ex) {
Logger.getLogger(ArquivosClienteBean.class.getName()).log(Level.SEVERE, null, ex);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Erro", "Erro ao carregar arquivo"));
}
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Arquivo", getArquivo().getFileName() + " está carregado. "
+ "tipo de conteúdo "+getArquivo().getContentType()+"tamanho"+getArquivo().getSize()));
} else {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Erro_ext", getArquivo().getFileName() + "somente extensão .pdf"));
}
}
else {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Erro", getArquivo().getFileName() + "selecione um arquivo!"));
}
}
public String editar() {
return "/restrito/cadastrararquivocliente";
}
public String excluir() {
ArquivosClienteRN arquivosClienteRN = new ArquivosClienteRN();
arquivosClienteRN.excluir(this.arquivosCliente);
this.lista = null;
return null;
}
public List<ArquivosCliente> getLista() {
if (this.lista == null) {
ArquivosClienteRN arquivosclienteRN = new ArquivosClienteRN();
this.lista = arquivosclienteRN.listar();
}
return lista;
}
public ArquivosCliente getArquivosCliente() {
return arquivosCliente;
}
public void setArquivosCliente(ArquivosCliente arquivosCliente) {
this.arquivosCliente = arquivosCliente;
}
public String getCaminho() {
return caminho;
}
public void setCaminho(String caminho) {
this.caminho = caminho;
}
}
XHTML
<h:outputLabel value="Arquivo:" />
<p:fileUpload value="#{arquivosClienteBean.arquivo}" mode="simple" update="growl" />
<p:commandButton value="Enviar" action="#{arquivosClienteBean.upload()}" />
<h:outputLabel/>
<h:form>
<p:growl id="growl" showDetail="true"/>
</h:form>
Web.xml
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>/uploaded</param-value>
</init-param>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>51200</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>C:\tmp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</filter-mapping>
pom.xml
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
Obrigada.
Sgrandini
Curtir tópico
+ 0
Responder
Posts
27/08/2015
Sgrandini
Continuo sem solução para este problema. Será que se trata de um caso impossível de se resolver????
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)