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:
Already tried to pass String
nomeArquivoSaida
for object?– DiegoAugusto
how can I do this?
– wladyband
Instead of passing getNomeArchvoSaida, pass the fileName and see if it works
– DiegoAugusto
Make a mistake when Voce tries to save in the bank?
– DiegoAugusto
It didn’t work using Filename, and it just doesn’t work and doesn’t generate error message on the console.
– wladyband
When you put one
System.out.println(nomeArquivoSaida)
it displays the message with the file path?– DiegoAugusto
The strange thing is that he demanded the full path, wants me to put the project on github for you to take a look?
– wladyband
Put it on, I’ll take a look.
– DiegoAugusto
Check the size of the field in the database as well.
– DiegoAugusto
there is a file at the root that has to click with start application to create the table news.
– wladyband
I get it, hand me the bill
– DiegoAugusto
that is the way >>>>> https://github.com/wladyband/VendeLancha/tree/master/VendeLancha
– wladyband
i put tb in the post
– wladyband
Are you already visualizing?
– wladyband
I’ll see now, I was having lunch
– DiegoAugusto