I have consulted most of the posts here on stackoverflow on uploading images in primefaces. With this help, I have been able to upload an image to a destination path statically specified in code as shown in this post. save image file in specific directory jsf primefaces project. This works fine.
However, I wish to upload an Image to a desitnation path specified in web.xml. This is because I want the path to be configurable even after the application is deployed. When I use ServletContext#getRealpath(), the return path is with in the myProject folder, but I want the destination path to be completely external to the project since I have found it as the best way. e.g E:/myUploads
This is my web.xml
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>51200</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>E:/myUploads</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
This is my bean.
public void handleFileUpload(FileUploadEvent event){
//get uploaded file from the event
UploadedFile uploadedFile = (UploadedFile) event.getFile();
//create an InputStream from the uploaded file
InputStream inputStr = null;
try
{
inputStr = uploadedFile.getInputstream();
} catch (IOException e) {
//log error
}
ServletContext servletContext = (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
String uploadPath = servletContext.getRealPath("");
File destFile = new File(uploadPath);
//use org.apache.commons.io.FileUtils to copy the File
try {
FileUtils.copyInputStreamToFile(inputStr, destFile);
} catch (IOException e) {
//log error
}
FacesMessage msg = new FacesMessage(event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
My desire is to save the Uploaded Image in E:/myUploads without having to say:
String destPath = "E:\\myUploads\\" + uploadedFile.getFileName();
I will be glad if you also show me how to display the uploaded images using
<p:graphicImage