How to record the images inside the project?

1

Greeting for all,

I'm working on a Java Web project that is using JSF with PrimeFaces, Maven, CDI with JPA.

My web application is successfully inserting the logs, the application is a news registry, and thank goodness the application is with the implementation of the upload working properly. The uploading approach I'm using is to write the image path in the database and put the images in a folder on the desktop

Currently my application is saving my images on this path;

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

My boss asked me to save the images inside the project, and I made a try as 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 end up saving in the wrong place as shown in the image below, this was the select in the database.

HowcanIsavethefileinthesuggestedpath?

Hereisaphotoofthestructureofmyproject.

    
asked by anonymous 31.07.2015 / 20:45

0 answers