How to Save Image in Database with JSF2

Asked

Viewed 889 times

0

I’m with a project JSF with the model DAO using Hibernate, and I’m having a hard time understanding how to save the image in the database.

I would just need to know what the method would look like to save an image in a class DAO and how he’d look in class Bean.

I found this video: https://www.youtube.com/watch?v=2b3KQApdOAU

For me this video does not serve because it saves on disk. I made these posts on the GUJ site but I did not get results: http://www.guj.com.br/39728-como-gravar-imagens-na-base-de-dados-em-jsf

This one got closer than he needed: http://www.guj.com.br/39230-jsf2---upload-de-imagens

This was a repository I found: https://github.com/wladyband/fileupload

This is the project I’m working for: https://github.com/wladyband/Drogaria/tree/master/Drogaria

And that’s the class I want to introduce: https://github.com/wladyband/Drogaria/blob/master/Drogaria/src/br/com/drogaria/domain/Produto.java

  • And specifically, what’s your question? You said you checked some projects, did you test the solution used on them? If so, did you have any problems?

1 answer

2

Since Voce didn’t post the code, see if this can help you:

@ManagedBean(name = "fileUploadMB")
@RequestScoped
public class fileUploadMB {
    public static byte[] arquivo;
    public static String nomeArquivo;
    private Ouvidoria ouvidoriaCadastro;



    public Ouvidoria getOuvidoriaCadastro() {
        if (ouvidoriaCadastro == null) {
            ouvidoriaCadastro = new Ouvidoria();
        }
        return ouvidoriaCadastro;
    }


    public void setOuvidoriaCadastro(Ouvidoria ouvidoriaCadastro) {
        this.ouvidoriaCadastro = ouvidoriaCadastro;
    }

    /* Método que faz o Upload dos arquivos */
    public void doUpload(FileUploadEvent fileUploadEvent) throws IOException {

        UploadedFile uploadedFile = fileUploadEvent.getFile();

        String fileNameUploaded = uploadedFile.getFileName();
        long fileSizeUploaded = uploadedFile.getSize();
        System.out.println(uploadedFile);

        // arquivo = uploadedFile.getContents();
        String infoAboutFile = "<br/> Arquivo recebido: <b>" + fileNameUploaded
                + "</b><br/>" + "Tamanho do Arquivo: <b>" + fileSizeUploaded
                + "</b>";
        //Mensagem exibida na tela
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.addMessage(null,
                new FacesMessage("Sucesso", infoAboutFile));

        arquivo = (IOUtils.toByteArray(uploadedFile.getInputstream()));
        nomeArquivo = uploadedFile.getFileName();

        ouvidoriaCadastro.setArquivo(arquivo);
        ouvidoriaCadastro.setNomeArquivo(fileNameUploaded);
        ouvidoriaCadastro.setNomeArquivo(nomeArquivo);

        System.out.println("Arquivo capturado" + arquivo);
        System.out.println("Nome do Arquivo" + nomeArquivo);

    }



}

On your xml page:

<p:outputLabel value="Selecione um Arquivo(Opcional):" />
            <p:fileUpload fileUploadListener="#{fileUploadMB.doUpload}"
                mode="advanced" showButtons="false" label="Enviar Arquvios.."
                auto="true" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.