How to write image to database with JSF2

0

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

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

I found this video: link

For me this video is not useful as it saves to disk. I made these postings on the GUJ website but did not get results: link

This one got closer than you needed: link

This was a repository I found: link

This is the project I'm working on: link

And this is the class I want to introduce to the modification:

    
asked by anonymous 07.05.2015 / 23:58

1 answer

2

Since you have not posted the code, see if this might 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" />
    
08.05.2015 / 13:14