Check file before upload with h: inputFile and javax.servlet.http.Part in JSF
I am working on a JSF web project that uses the h: inputFile facility to upload files. I am binding the h: inputFile value to a base bean property of type javax.servelt.http.Part
<h:inputFile id="inputfile" validator="#{bean.validateFile}" value="#{bean.part}"/>
Also I use the following file validation strategy in the same bean backing:
public void validateFile(FacesContext con, UIComponent comp, Object value) {
Part p = (Part) value;
List<FacesMessage> list = new ArrayList<>();
if (p.getSize() == 0) {
list.add(new FacesMessage("File Size too small"));
}
if (p.getSize()>MAXSIZE) {
list.add(new FacesMessage("File Size too Big"));
}
if (!list.isEmpty()) {
throw new ValidatorException(list);
}
}
My question is: Does the Part interface use the whole file that will be uploaded to the server and THEN it will receive confirmation (i.e. its size) when the form is submitted?
As I experience this on localhost (Glass fish v.4), I see a message in the left corner of the Chrome browser indicating the file upload process, and after that, in case of a file validation error, I get a confirmation message.I am worried in real life situations (e.g. slow internet connections), this is a rather inconvenient way to send a size check error after uploading a file!
Please help me and if this is the way I am guessing please post an alternative solution.
Thanks in advance!
source to share