DUVIDA : Como fazer o upload de imagem e exibir em uma Pagina JSF

1 resposta
R

Consegui fazer o upload e salvar a imagem em um diretorio ulitizando JSF. No entanto para exibir a imagem pensei que era apenas uma a tag mas não funciona.

Pagina do UPLOAD :

<ui:composition template="/WEB-INF/template/LayoutPadrao.xhtml"
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui">
 
       
        <ui:define name="titulo">Novo pedido </ui:define>
 
        <ui:define name="corpo">
                <h:body>
                 <h:form enctype="multipart/form-data">
        <p:fileUpload fileUploadListener="#{teste.upload}" fileLimit="10" fileLimitMessage="Excedido Limite de arquivos"
           cancelLabel="Cancelar" label="Arquivo" uploadLabel="Anexar"
 
           invalidFileMessage="Somente arquivos .jpg, .png ou .gif"
           allowTypes="/(\.|\/)(gif|jpe?g|png)$/" value="#{teste.file}"
           mode="advanced" skinSimple="true" />
        <p:commandButton value="Enviar" ajax="false" />
        </h:form>
                        </h:body>
        </ui:define>
</ui:composition>

BEAN :

package com.pedidovenda.controller;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.ServletContext;
 
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
 
import com.pedidovenda.service.NegocioException;
import com.pedidovenda.util.jsf.FacesUtil;
import com.pedidovenda.util.jsf.SessionUtil;
 
@Named
@RequestScoped
public class Teste {
 
    private UploadedFile file;
 
    public UploadedFile getFile() {
        return file;
    }
 
    public void setFile(UploadedFile file) {
        this.file = file;
    }
 
    public void upload(FileUploadEvent event) {
        file = event.getFile();
 
        if (file != null) {
                 try {
                         
//                       FacesContext facesContext = FacesContext.getCurrentInstance();  
//                       ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();  
//                       String caminho = scontext.getRealPath("/upload/");  
                       
                         
                         String caminho = "/home/rudson/upload/";
                         
                         
                         
            File file1 = new File(caminho,file.getFileName());
           
                FileOutputStream fos = new FileOutputStream(file1);
                fos.write(event.getFile().getContents());
                fos.close();
 
               FacesUtil.addInfoMessage(
                        file.getFileName() + " anexado com sucesso");
               
                SessionUtil.setParam("comprovanteCaminho", caminho+file.getFileName());
            } catch (FileNotFoundException e) {
                NegocioException n = new NegocioException(e+"");
               
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                                System.out.println(e);
                        }
 
 
        }
    }
}

Pagina de exibição da imagen :

<ui:composition template="/WEB-INF/template/LayoutPadrao.xhtml"
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui">
 
       
        <ui:define name="titulo">Novo pedido </ui:define>
 
        <ui:define name="corpo">
                <h:body>
 
 
 
<img src="home/upload/imagen.jpg" />
</h:body>
        </ui:define>
</ui:composition>

Por favor alguém me ajude o sistema deveria esta no ar .

1 Resposta

ViniGodoy

Não duplique tópicos:

Criado 30 de maio de 2015
Ultima resposta 31 de mai. de 2015
Respostas 1
Participantes 2