How to use the upload approach saving the path in the bank?

Asked

Viewed 192 times

1

I created a Java web project that is using JSF with Primefaces, Maven, CDI with JPA. My web application is successfully entering the records, the application is a registration of news, where it is also necessary to insert a photo, the approach of upload used is to save the image path in the bank.

I was able to complete half of the implementation of upload, i.e., I was successful in saving the image in the folder, however I was unable to save the folder path in the bank.

Everything is working, because of this I will only put pieces of code to get an idea. Package from Repository:

@Inject
private EntityManager manager;

public Noticia guardar(Noticia noticia) {
    return manager.merge(noticia);
}

Bundle Model is all mapped correctly with get and set:

private Long id;
private String titulo_noticia;
private Date data_noticia;
private String foto_noticia;
private String desc_noticia;

Bundle Controller:

@Named
@ViewScoped
public class CadastroNoticiaBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private Noticia noticia;
    private Part arquivo;
    private String nomeArquivoSaida;

    @Inject
    private CadastroNoticiaService cadastroNoticiaService;

    public CadastroNoticiaBean() {
        limpar();
    }

    public void limpar(){
        noticia = new Noticia();
    }

    public void salvar() {
        this.noticia = cadastroNoticiaService.salvar(this.noticia);
        upload();
        limpar();
        FacesUtil.addInfoMessage("Noticia salva com sucesso! ");
    }

    public void upload() {

         nomeArquivoSaida = "C:/workspace Web/Projetos Profissionais/Fotos para teste/" + arquivo.getSubmittedFileName();   

         try (InputStream is = arquivo.getInputStream();
                OutputStream out = new FileOutputStream(nomeArquivoSaida)) {

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = is.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }

//tentei colocar esse código abaixo para conseguir fazer o insert do caminh do
//banco, mas não tivesse resultado
            // preciso muito de ajuda para saber como resolver.
            noticia.setFoto_noticia(getNomeArquivoSaida());

        } catch (IOException e) {
            FacesUtil.addErrorMessage("Erro ao enviar arquivo.");
        }
    }

    public Noticia getNoticia() {
        return noticia;
    }

    public Part getArquivo() {
        return arquivo;    
    }

    public void setArquivo(Part arquivo) {
        this.arquivo = arquivo;
    }

    public String getNomeArquivoSaida() {

        return nomeArquivoSaida;
    }

    public void setNomeArquivoSaida(String nomeArquivoSaida) {
        this.nomeArquivoSaida = nomeArquivoSaida;
    }
}

This is the piece of code on the XHTML page that involves the problem:

<p:outputLabel value="Foto" />
<h:inputFile value="#{cadastroNoticiaBean.arquivo}"/>

I believe it is a simple change, I just need to know what it is. I took a test of debug:

The project on Github.

  • Already tried to pass String nomeArquivoSaida for object?

  • how can I do this?

  • Instead of passing getNomeArchvoSaida, pass the fileName and see if it works

  • Make a mistake when Voce tries to save in the bank?

  • It didn’t work using Filename, and it just doesn’t work and doesn’t generate error message on the console.

  • When you put one System.out.println(nomeArquivoSaida) it displays the message with the file path?

  • The strange thing is that he demanded the full path, wants me to put the project on github for you to take a look?

  • Put it on, I’ll take a look.

  • Check the size of the field in the database as well.

  • there is a file at the root that has to click with start application to create the table news.

  • I get it, hand me the bill

  • that is the way >>>>> https://github.com/wladyband/VendeLancha/tree/master/VendeLancha

  • i put tb in the post

  • Are you already visualizing?

  • I’ll see now, I was having lunch

Show 10 more comments

1 answer

2

The problem is in your saving method:

 public void salvar() {
        this.noticia = cadastroNoticiaService.salvar(this.noticia);
        upload();
        limpar();
        FacesUtil.addInfoMessage("Noticia salva com sucesso! ");
    }

Note that you make the inclusion in the database and after the inclusion you upload it.

Do it that way:

 public void salvar() {
        upload();
        this.noticia = cadastroNoticiaService.salvar(this.noticia);
        FacesUtil.addInfoMessage("Noticia salva com sucesso! ");
        limpar();

}

Upload must be done first so that the attribute nomeArquivoSaida is not void at the time of inclusion.

  • Our man, thank you so much, it all worked out, really worth.

  • 2

    For nothing expensive, do not forget to mark the answer as correct so you can help someone with a similar question.

Browser other questions tagged

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