oi pessoal tudo bem…estou com um problema com upload de arquivos do primefaces, estou usando postgres e hibernate
segui o tutorial da MeninaJava
http://meninajava.blogspot.com.br/2012/01/fileupload-com-jsf-primefaces-hibernate.html
Nao grava no banco(fica null o caminho), e a imagem nao vai para o diretorio(fotos)
LOG DO APACHE:
Hibernate:
/* criteria query / select
this_.id_cat as id1_5_0_,
this_.nome_cat as nome2_5_0_
from
categoria this_
Hibernate:
/ criteria query / select
this_.id_cat as id1_5_0_,
this_.nome_cat as nome2_5_0_
from
categoria this_
Hibernate:
select
nextval (‘produto_id_produto_seq’)
=========== ENTROU NO METODO GRAVAR=================
ENTROU NO TRY DO METODO GRAVAR
java.lang.NullPointerException
==============CAIU NO CATCH DO METODO GRAVAR==================
ERRO toString(): java.lang.NullPointerException
ERRO getLocalizedMessage: null
ERRO getCause(): null
ERRO getStackTrace():[Ljava.lang.StackTraceElement;@3e78b671
ERRO getClass(): class java.lang.NullPointerException
Hibernate:
/ insert br.com.ecommerceetelj.model.Produto
*/ insert
into
produto
(id_cat, desconto_produto, descricao_produto, imagem_produto, nome_produto, preco_produto, qtd_produtodisponivel_produto, id_produto)
values
(?, ?, ?, ?, ?, ?, ?, ?)
xml
<!--Configuração fileUpload -->
<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>C:/temp</param-value>
</init-param>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>10000</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
UploadArquivo.class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.ecommerceetelj.bean;
import java.io.File;
import java.io.FileOutputStream;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import org.primefaces.event.FileUploadEvent;
/**
*
* @author Vagner Sistemas
*/
public class UploadArquivo{
private String caminho;
private byte[] arquivo;
private String nome;
public UploadArquivo() {
}
public String getNome() {
return nome;
}
public String getRealPath() {
ExternalContext externalContext =
FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse response =
(HttpServletResponse) externalContext.getResponse();
FacesContext aFacesContext = FacesContext.getCurrentInstance();
ServletContext context =
(ServletContext) aFacesContext.getExternalContext().getContext();
return context.getRealPath("/");
}
public void fileUpload(FileUploadEvent event, String type, String diretorio) {
System.out.println("ENTROU METODO FILEUPLOAD");
System.out.println("VALOR EVENT: "+event);
System.out.println("TIPO: "+type);
System.out.println("DIRETORIO: "+diretorio);
try {
this.nome = new java.util.Date().getTime() + type;
System.out.println("VALOR VARIAVEL NOME: "+this.nome);
this.caminho = getRealPath() + diretorio + getNome();
System.out.println("VALOR VARIAVEL CAMINHO: "+this.caminho);
this.arquivo = event.getFile().getContents();
System.out.println("VALOR VARIAVEL ARQUIVO: "+this.arquivo);
File file = new File(getRealPath() + diretorio);
file.mkdirs();
System.out.println("VALOR FILE: "+file);
} catch (Exception ex) {
System.out.println("Erro no upload do arquivo" + ex);
}
}
public void gravar(){
System.out.println("=========== ENTROU NO METODO GRAVAR=================");
try {
System.out.println("ENTROU NO TRY DO METODO GRAVAR");
FileOutputStream fos;
fos = new FileOutputStream(this.caminho);
System.out.println("VALOR VARIVAEL CAMINHO NO METODO GRAVAR: "+this.caminho);
fos.write(this.arquivo);
fos.close();
} catch (Exception ex) {
System.out.println(ex);
System.out.println("ERRO toString(): "+ex.toString());
System.out.println("ERRO getLocalizedMessage: "+ex.getLocalizedMessage());
System.out.println("ERRO getCause(): "+ex.getCause());
System.out.println("ERRO getStackTrace():"+ex.getStackTrace());
System.out.println("ERRO getClass(): "+ex.getClass());
}
}
}
ProdutoBean
[code]
//METODO PARA O FIPLEUPLOAD=================================================
public void uploadAction (FileUploadEvent event){
this.arquivo.fileUpload(event, “.jpg”, “/fotos/”);
this.produto.setImagemProduto(this.arquivo.getNome());
}
public String salvar(){
ProdutoRN produtoRN = new ProdutoRN();
produtoRN.salvar(produto);
//chama metoda da classe UploadArquivo
this.arquivo.gravar();
this.produto = new Produto();
this.arquivo = new UploadArquivo();
//retorna para a pagina sucessoproduto.xhtml
return destinoSalvarProduto;
}
[/code]