I want to upload an image with PrimeFaces and jsf. Image is not getting uploaded even PrimeFaces tag looks like good also validating the image size and type.
My web.xml configuration
<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>5120000</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>D:\tmp</param-value>
</init-param>
</filter>
My PrimeFaces code in my xhtml
<p:fileUpload fileUploadListener="#{itemBean.handleFileUpload}" mode="advanced" dragDropSupport="false"
update="messages" sizeLimit="10000000" fileLimit="1" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
<p:growl id="messages" showDetail="true"/>
ItemBean code
private String destination="D:\\tempt\\";
public void handleFileUpload(FileUploadEvent event) {
System.out.println("going to file upload");
FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
// Do what you want with the file
try {
copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void copyFile(String fileName, 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.out.println("New file created!");
} catch (IOException e) {
System.out.println(e.getMessage());
}
............................