Noite Galera
Então Thyago não sei se te ajuda, mas vamos lá.
Estou usando Glassfish 3.1, Primefaces 2.2.1 e EJB 3, estou passando a configuração as vezes pode não bater como e com o que está desenvolvendo.
XHTML
<h:form id="UpImagem" enctype="multipart/form-data">
<p:messages showDetail="true"/>
<p:fileUpload label="Selecionar..." sizeLimit="9999999" fileUploadListener="#{AnexoMB.fileUploadAction}" auto="true" allowTypes="*.jpg;,*.txt;,*.doc;,*.pdf;,*.gif;" multiple="false"/>
</h:form>
WEB.XML
<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:/arquivo/imagem/</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>
É estranho pq só com isso já ta salvando, mas não ta salvando como eu quero, ele cria uma arquivo TMP dentro da pasta “C:/arquivo/imagem/” e mesmo qualquer alteração que estou fazendo no ManagedBean não surge efeito, se tiver uma luz heheheh.
Mas abaixo segue a minha parte incomplete o qual nem sei se está funcionando ou não.
AnexoFachada
@Stateless
public class AnexoFachada {
@EJB
private AnexoDAO anexoDAO;
private Anexo anexo;
public void inserir(Anexo anexo) {
anexoDAO.inserir(anexo);
}
public Anexo recuperarPorId(Integer id) {
return anexoDAO.recuperarPorId(id);
}
public void inserir(byte[] img) {
anexoDAO.inserir(anexo);
}
}
AnexoDAO
@Stateless
public class AnexoDAO {
@PersistenceContext
private EntityManager em;
public void inserir(Anexo anexo) {
em.persist(anexo);
em.merge(anexo);
}
public Anexo recuperarPorId(Integer id) {
return em.find(Anexo.class, id);
}
}
Anexo(bean)
@Entity
@Table(name = "anexo")
@NamedQueries({
@NamedQuery(name = "Anexo.findAll", query = "SELECT a FROM Anexo a"),
@NamedQuery(name = "Anexo.findByAnexoId", query = "SELECT a FROM Anexo a WHERE a.anexoId = :anexoId"),
@NamedQuery(name = "Anexo.findByCaminho", query = "SELECT a FROM Anexo a WHERE a.caminho = :caminho"),
@NamedQuery(name = "Anexo.findByNome", query = "SELECT a FROM Anexo a WHERE a.nome = :nome")})
public class Anexo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "anexo_id", nullable = false)
@SequenceGenerator(name="Anexo_Generator", sequenceName="anexo_sequence", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="Anexo_Generator")
private Integer anexoId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "caminho", nullable = false, length = 255)
private String caminho;
@Size(max = 255)
@Column(name = "nome", length = 255)
private String nome;
@Lob
@Column(name = "arquivo")
private byte[] arquivo;
public Anexo() {
}
public Anexo(Integer anexoId) {
this.anexoId = anexoId;
}
public Anexo(Integer anexoId, String caminho) {
this.anexoId = anexoId;
this.caminho = caminho;
}
public Integer getAnexoId() {
return anexoId;
}
public void setAnexoId(Integer anexoId) {
this.anexoId = anexoId;
}
public String getCaminho() {
return caminho;
}
public void setCaminho(String caminho) {
this.caminho = caminho;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public byte[] getArquivo() {
return arquivo;
}
public void setArquivo(byte[] arquivo) {
this.arquivo = arquivo;
}
@Override
public int hashCode() {
int hash = 0;
hash += (anexoId != null ? anexoId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Anexo)) {
return false;
}
Anexo other = (Anexo) object;
if ((this.anexoId == null && other.anexoId != null) || (this.anexoId != null && !this.anexoId.equals(other.anexoId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "negocio.entidade.Anexo[ anexoId=" + anexoId + " ]";
}
}
ManagedBean
Acredito que meu MB está errado, porque parece que não chama o método e não persiste no banco.
@ManagedBean(name = "anexoMB")
@SessionScoped
public class AnexoMB implements Serializable {
private Anexo anexo;
private ListDataModel anexos;
private byte[]img;
private StreamedContent file;
@EJB
private AnexoFachada anexoFachada;
public void AnexoMB(){}
public Anexo getAnexo() {
return anexo;
}
public void setAnexo(Anexo anexo) {
this.anexo = anexo;
}
public StreamedContent getFile() {return file;}
public void setFile(StreamedContent file) {this.file = file;}
public void recuperarAnexo() {
this.anexo = (Anexo) this.anexos.getRowData();
}
public String montarPaginaParaInsercao() {
this.anexo = new Anexo();
return "/anexo/InserirAnexo";
}
public void fileUploadAction(FileUploadEvent e) throws IOException{
Anexo img = null;
try{
UploadedFile arq = e.getFile();
InputStream in = new BufferedInputStream(arq.getInputstream());
String caminho = FacesContext.getCurrentInstance().getExternalContext().getRealPath("C:/arquivo/imagem/" + e.getFile().getFileName());
File fileAnexo = new File(caminho);
fileAnexo.getParentFile().mkdirs();
FileOutputStream fout = new FileOutputStream(caminho);
while (in.available() != 0) {
fout.write(in.read());
}
fout.close();
anexo = new Anexo();
anexo.setCaminho(caminho);
anexoFachada.inserir(img);
} catch (Exception ex) {
ex.getMessage();
if (anexo != null) {
new File(anexo.getCaminho()).delete();
}
}
}
}