0

Hello i'm new at JSF and primefacess and i want to upload an image and save it in a folder in my project

when Execute all the code pass properly but when i check the save directory i don't find the image that i saved.

//Java Code private UploadedFile file;

public UploadedFile getFile() {
    return file;
}



public void setFile(UploadedFile file) {
    this.file = file;
}



public void upload() {
    if(file != null) {
        try {
                FacesContext context = FacesContext.getCurrentInstance();
                ServletContext scontext = (ServletContext)context.getExternalContext().getContext();
                String rootpath = scontext.getRealPath("/");
                File fileImage=new File(rootpath+"upload\\temp\\text.png");
                InputStream inputStream=file.getInputstream();
                SaveImage(inputStream,fileImage);

                FacesMessage message = new FacesMessage(rootpath);
                FacesContext.getCurrentInstance().addMessage(null, message);
            }
        catch(IOException e) {
            e.printStackTrace();
            FacesMessage message = new FacesMessage("There was a probleme your file was not uploaded.",e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }
}

public void SaveImage(InputStream inputStream, File ImageFile) throws IOException {
    OutputStream outputStream=new FileOutputStream(ImageFile);
    IOUtils.copy(inputStream, outputStream);
}

//XHTML Code

<h:form enctype="multipart/form-data">
    <p:growl id="messages" showDetail="true" />
    <p:fileUpload value="#{userBean.file}" mode="simple" skinSimple="true"/>
    <p:commandButton value="Submit" ajax="false" actionListener="#{userBean.upload}" />
</h:form>
Ayoub EL Abassi
  • 41
  • 3
  • 11

2 Answers2

2

It might be because of the OS you are running on and it does not recognize the path correctly when you are using \ :

File fileImage=new File(rootpath+"upload\\temp\\text.png");

Replace this line with:

File fileImage=new File(rootpath+"upload"+File.separator+"temp"+File.separator+"text.png");

This is a good practice when you are working with file paths across different platforms.

Raul Cuth
  • 2,269
  • 1
  • 12
  • 17
0

You can use this method :

You can get the inputstream using the FileUploadEvent like :

InputStream in = event.getFile().getInputstream() ; 

public void upload(String fileName, String destination, InputStream in) {
    try {

        // write the inputStream to a FileOutputStream            
        OutputStream out = new FileOutputStream(new File(destination + "/" + fileName));
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = in.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        in.close();
        out.flush();
        out.close();
        System.err.println("New file created!");
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

}
Yagami Light
  • 1,756
  • 4
  • 19
  • 39