2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

How to validate a URL using java

I have a URL as below formats


http://www.example.com

https://www.example.com

http://example.com

https://example.com


How to validate these?

userimage

Probably you would like to use below code

/**
   * Will validate the URL pattern.
   * 
   * @param url
   * @return
   */
   public static boolean isURL(String url) {
      String urlPattern = "^http(s{0,1})://?(www.)?[a-zA-Z0-9_/-.]+.([A-Za-z/]{2,5})[a-zA-Z0-9_/&?=-.~%]*";
      return url.matches(urlPattern);
   }
userimage

try using below code to validate:


try {
   new URL("Your URL").toURI();
} catch (MalformedURLException | URISyntaxException e) {
   e.getMessage();
}

This will help u for sure.

Answer is