Get port from a URI in Java

Description

The following code shows how to get port from a URI.

Example


import java.net.URI;
/*from   w ww .  j  a v  a 2s  .com*/
public class Main {

  public static void main(String args[]) throws Exception {

    URI u = new URI("http://www.java2s.com");
      System.out.println("The port is " + u.getPort());    
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode