Upload the image into the context of the java jsf project

Asked

Viewed 156 times

0

I have a project in JSF put. One of the parts is to add the image to the product by making the upload of the image. That’s why I’m using the primefaces p:fileUpload, it works, but the image is "played" to the PC folder and not to the project folder, I need this image to go to the folder inside the project, in WebContent-Images.

Upload method:

public void upload(FileUploadEvent event) {
        UploadedFile uploadedFile = event.getFile();
        try {
            File file = new File("", uploadedFile.getFileName());
            OutputStream out = new FileOutputStream(file);
            out.write(uploadedFile.getContents());
            out.close();
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage("Upload completo", "O arquivo " + uploadedFile.getFileName() + " foi salvo!"));
        } catch (IOException e) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro", e.getMessage()));
        }
    }

1 answer

0

Good morning,

I don’t think it’s a good copy to the project folder, since every time you upgrade the project you will lose the images that have already been loaded.

Maybe you’d better put it in a specific folder and read the image look into this same folder

To change the read path just pass the time to create the File so

public void upload(FileUploadEvent event) {
        UploadedFile uploadedFile = event.getFile();
        try {
            File file = new File("/tmp/pasta_imagens/", uploadedFile.getFileName());
            OutputStream out = new FileOutputStream(file);
            out.write(uploadedFile.getContents());
            out.close();
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage("Upload completo", "O arquivo " + uploadedFile.getFileName() + " foi salvo!"));
        } catch (IOException e) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro", e.getMessage()));
        }
    }
  • blz, thank you for your attention.

Browser other questions tagged

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