How to write the folder path in the database?

0

Greeting for all,

I created an application that is saving the registry in the database, as shown in the figure;

Thisapplicationissucceedinginsavingthetitle,dateanddescription,andisabletouploadtheimagetothefolderthatisdetermined,howeverIwanttosavethefolderpathinthedatabase,howdoIdothat?

Hereisthenewsentity;

packagebr.com.vendelancha.model;importjava.io.Serializable;importjava.util.Date;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.Id;importjavax.persistence.Temporal;importjavax.persistence.TemporalType;importjavax.validation.constraints.NotNull;importjavax.validation.constraints.Size;importorg.hibernate.validator.constraints.NotBlank;@EntitypublicclassNoticiaimplementsSerializable{/****/privatestaticfinallongserialVersionUID=1L;privateLongid;privateStringtitulo_noticia;privateDatedata_noticia;privateStringfoto_noticia;privateStringdesc_noticia;@Id@GeneratedValuepublicLonggetId(){returnid;}publicvoidsetId(Longid){this.id=id;}@NotBlank@Size(max=80)@Column(nullable=false,length=50)publicStringgetTitulo_noticia(){returntitulo_noticia;}publicvoidsetTitulo_noticia(Stringtitulo_noticia){this.titulo_noticia=titulo_noticia;}@Temporal(TemporalType.TIMESTAMP)@Column(nullable=false)publicDategetData_noticia(){returndata_noticia;}publicvoidsetData_noticia(Datedata_noticia){this.data_noticia=data_noticia;}publicStringgetFoto_noticia(){returnfoto_noticia;}publicvoidsetFoto_noticia(Stringfoto_noticia){this.foto_noticia=foto_noticia;}@NotBlank@Column(columnDefinition="text")
    public String getDesc_noticia() {
        return desc_noticia;
    }

    public void setDesc_noticia(String desc_noticia) {
        this.desc_noticia = desc_noticia;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Noticia other = (Noticia) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

}

Here is the XHTML page;

<ui:composition template="/WEB-INF/template/LayoutSystem.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:h="http://xmlns.jcp.org/jsf/html">



    <!-- xmlns:h="http://java.sun.com/jsf/html" -->

    <ui:define name="titulo">Novas Notícias</ui:define>

    <ui:define name="corpo">

        <h:form id="form" enctype="multipart/form-data">
            <h1>Novas Notícias</h1>

            <p:messages autoUpdate="true" closable="true" />

            <p:toolbar style="margin-top: 20px">
                <p:toolbarGroup>
                    <p:button value="Novo" />


                    <h:commandButton value="Salvar" id="botaoSalvar"
                        action="#{cadastroNoticiaBean.salvar}">
                        <f:ajax execute="@form" render="@all"/>
                    </h:commandButton>

                </p:toolbarGroup>


                <p:toolbarGroup align="right">
                    <p:button value="Pesquisa"
                        outcome="/noticias/PesquisaNoticias.xhtml" />
                </p:toolbarGroup>
            </p:toolbar>

            <div id="wrap">

                <div class="left-sidebar">
                    <p:panelGrid columns="2" id="painel1"
                        style="width: 50%; margin-top: 20px" columnClasses="rotulo, campo">


                        <p:outputLabel value="Titulo" for="titulo" />
                        <p:inputText id="titulo" size="20" maxlength="50"
                            value="#{cadastroNoticiaBean.noticia.titulo_noticia}" />

                        <p:outputLabel value="Data" for="data_noticia" />
                        <p:calendar id="data_noticia" pattern="dd/MM/yyyy"
                            value="#{cadastroNoticiaBean.noticia.data_noticia}" />



                        <p:outputLabel value="Descrição" for="descricao" />

                        <p:inputText id="descricao" size="20" maxlength="250"
                            value="#{cadastroNoticiaBean.noticia.desc_noticia}" />



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




                    </p:panelGrid>
                </div>
            </div>
        </h:form>
    </ui:define>
</ui:composition>

Here's my Bean class;

package br.com.vendelancha.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.Part;

import br.com.vendelancha.model.Noticia;
import br.com.vendelancha.service.CadastroNoticiaService;
import br.com.vendelancha.util.jsf.FacesUtil;

@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);
            }


        } 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;
    }

}
    
asked by anonymous 30.07.2015 / 13:37

0 answers