Build URL in java

前端 未结 5 1480
故里飘歌
故里飘歌 2021-02-05 00:56

Trying to build http://IP:4567/foldername/1234?abc=xyz. I don\'t know much about it but I wrote below code from searching from google:

import java.n         


        
5条回答
  •  醉话见心
    2021-02-05 01:31

    In general non-Java terms, a URL is a specialized type of URI. You can use the URI class (which is more modern than the venerable URL class, which has been around since Java 1.0) to create a URI more reliably, and you can convert it to a URL with the toURL method of URI:

    String protocol = "http";
    String host = "example.com";
    int port = 4567;
    String path = "/foldername/1234";
    String auth = null;
    String fragment = null;
    URI uri = new URI(protocol, auth, host, port, path, query, fragment);
    URL url = uri.toURL();
    

    Note that the path needs to start with a slash.

提交回复
热议问题