I'm uploading images using <p:fileUpload>
as follows.
<p:outputLabel for="txtCatImage" value="#{messages['category.image']}"/>
<p:fileUpload id="txtCatImage" mode="advanced"
dragDropSupport="true" required="true"
sizeLimit="1000000" fileLimit="1" multiple="false"
cancelLabel="#{messages['fileupolad.cancelLabel']}"
label="#{messages['fileupolad.label']}"
uploadLabel="#{messages['fileupolad.uploadLabel']}"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
invalidFileMessage="#{messages['fileupolad.invalidFileMessage']}"
invalidSizeMessage="#{messages['fileupolad.invalidSizeMessage']}"
fileLimitMessage="#{messages['fileupolad.fileLimitMessage']}"
fileUploadListener="#{categoryManagedBean.fileUploadListener}"/>
<p:message for="txtCatImage" showSummary="false"/>
<p:commandButton id="btnSubmit" update="panel messages"
actionListener="#{categoryManagedBean.insert}"
value="#{messages['button.save']}"/>
fileUploadListener
in the corresponding managed bean decorated with @ViewScoped
is as follows.
//This is just a utility method and can be placed anywhere in the application.
private static boolean validateImageDimensions(byte[] bytes) throws IOException {
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(bytes));
return bufferedImage.getHeight()>=750 || bufferedImage.getWidth()>=650;
}
public void fileUploadListener(FileUploadEvent event) throws IOException {
UploadedFile uploadedFile = event.getFile();
byte[] bytes = IOUtils.toByteArray(uploadedFile.getInputstream());
if(!Utility.validateImageDimensions(bytes)) {
FacesContext context = FacesContext.getCurrentInstance();
context.validationFailed();
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Message summary", "Error message");
FacesContext.getCurrentInstance().addMessage(event.getComponent().getClientId(context), message);
}
else {//Do something.}
}
The listener of <p:commandButton>
is as follows which should not be invoked, if validation in fileUploadListener()
fails.
public void insert() {
//Code to invoke an EJB to insert a new row along with the uploaded file.
}
If if(!Utility.validateImageDimensions(bytes))
is evaluated to true then, the action listener of <p:commandButton>
(the insert()
method above) should not be invoked but it is invoked and this kind of validation implies no effect at all.
As already stated, PrimeFaces file upload validators don't work.
What am I doing wrong here? What is the way to validate dimensions of an uploaded image?