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>