-1

I have a problem with below if condition.

if(!(type=="image/jpeg" || type=="image/png" || type=="image/gif")){
    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Only jpeg, png or gif", ""));
}

When type is image/jpeg, it still throws ValidatorException. When I hardcode type directly to image/jpeg, it works. So I hope that you help me, because I really don't know where is problem.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
S.Roman
  • 23
  • 4
  • Problem in if statement, you need to debug with sop and find out in String type = file.getContentType(); what is value of type then need to check same value in if statement with equals() method – Piyush Gupta Feb 19 '16 at 04:19
  • Thanks, it's working – S.Roman Feb 19 '16 at 13:12

1 Answers1

0

I tested your code and found exception that why you are not getting correct output, you need to change

if(!(type.equals("image/jpeg") || type.equals("image/png") || type.equals("image/gif")){
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Only jpeg, png or gif", ""));
        }

NOTE: First you need to debug with System.out.println("Type value: "+type); that which output format is coming then need to put same format to check validation in if statement like type.equals("jpeg") or type.equals("image/jpeg").

It is working for me, I hope it will work for you also.

Piyush Gupta
  • 2,181
  • 3
  • 13
  • 28