fork download
  1. // Separar una URL en sus partes
  2. // https://es.stackoverflow.com/q/131417/127
  3.  
  4. import java.net.URL;
  5. import java.net.MalformedURLException;
  6.  
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. String prueba = "http://w...content-available-to-author-only...g.org:8080/forum/index.php?busq=abc#sec1";
  12.  
  13. try{
  14. URL miUrl = new URL(prueba);
  15. System.out.println("protocolo = " + miUrl.getProtocol());
  16. System.out.println("autoridad = " + miUrl.getAuthority());
  17. System.out.println("dominio = " + miUrl.getHost());
  18. System.out.println("puerto = " + miUrl.getPort());
  19. System.out.println("ruta = " + miUrl.getPath());
  20. System.out.println("búsqueda = " + miUrl.getQuery());
  21. System.out.println("archivo = " + miUrl.getFile());
  22. System.out.println("ancla = " + miUrl.getRef());
  23. System.out.println("URL inválida");
  24. }
  25. }
  26. }
Success #stdin #stdout 0.04s 4575232KB
stdin
Standard input is empty
stdout
protocolo = http
autoridad = www.devbg.org:8080
dominio   = www.devbg.org
puerto    = 8080
ruta      = /forum/index.php
búsqueda  = busq=abc
archivo   = /forum/index.php?busq=abc
ancla     = sec1