How do you save images within the project?

Asked

Viewed 452 times

1

Greeting to all,

I am working on 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 news registration, and thank God the application is with the upload implementation working properly. The upload approach I’m using is to record the image path in the database and place the images in a desktop folder

Currently my application is saving my images in this way;

C:/workspace Web/Projetos Profissionais/Fotos para teste/

My boss requested that I save the images inside the project, and I made a attempt as shown below;

    public String getRealPath() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

        FacesContext aFacesContext = FacesContext.getCurrentInstance();
        ServletContext context = (ServletContext) aFacesContext.getExternalContext().getContext();

        return context.getRealPath("/resources/images/");
    }


    public void upload() {

//      "C:/workspace Web/Projetos Profissionais/Fotos para teste/"

        this.nomeArquivoSaida = getRealPath() + 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);
            }


            noticia.setFoto_noticia(getNomeArquivoSaida());

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

But with this line of code ends up saving in the wrong place as shown in the image below, this was the select in the database.

inserir a descrição da imagem aqui

How do I manage to save the file in the suggested path?

here is a photo of the structure of my project.

inserir a descrição da imagem aqui

No answers

Browser other questions tagged

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