0

I have this code

<h:inputFile id="id_logo" value="#{organizercontroller.currentLogo}" validator="#{organizercontroller.validateLogo}"/>
<h:message id="id_logo_message" for="id_logo" />

and I want to validate the inputFile with the method validateLogo.

public void validateLogo(FacesContext context, UIComponent comp, Object value) throws ValidationException {
    Part logo = (Part) value;
    try (InputStream input = logo.getInputStream()) {
        if (input.available() > MAX_UPLOAD_SIZE) {
            String message = "Die Datei darf max. " + (int) MAX_UPLOAD_SIZE / 1000000 + " MB betragen";
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message));
        }
    } catch (Exception e) {
          e.printStackTrace();
    }
}

When I submit my form, the validation message is in my console and not in the <h:message> tag. What am I doing wrong?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
chocolate cake
  • 2,129
  • 5
  • 26
  • 48
  • Important note: [`input.available()` doesn't do what you think it does](http://stackoverflow.com/questions/26672592/why-does-an-image-in-graphicimage-not-load-fully-primefaces-mobile/26730007#26730007). – BalusC May 20 '15 at 18:59
  • `IOUtils.toByteArray(input).length` returns the same value as `input.available()` – chocolate cake May 21 '15 at 08:23
  • That will happen if the entire file fits in hardware buffer. You should in your software however absolutely not rely on that this is true for every file and/or hardware being used. Carefully read that link once again. – BalusC May 21 '15 at 22:45

0 Answers0