/*
URL网络编程
1.URL:统一资源定位符,对应着互联网的某一资源地址
2.格式:
http://localhost:8080/examples/beauty.jpg?username=Tom
协议 主机名 端口号 资源地址 参数列表
*/
public class URLTest {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/examples/beauty.jpg?username=Tom");
//public String getProtocol() 获取该URL的协议名
System.out.println(url.getProtocol());
//public String getHost() 获取该URL的主机名
System.out.println(url.getHost());
//public String getPort() 获取该URL的端口号
System.out.println(url.getPort());
//public String getPath() 获取该URL的文件路径
System.out.println(url.getPath());
//public String getFile() 获取该URL的文件名
System.out.println(url.getFile());
//public String getQuery() 获取该URL的查询名
System.out.println(url.getQuery());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
[网络编程]URL类的理解与实例化
最新推荐文章于 2025-12-28 16:49:38 发布
本文详细解读了URL的构成,包括协议、主机名、端口号、路径和查询参数,并通过Java示例展示了如何使用`java.net.URL`类进行操作。

172万+

被折叠的 条评论
为什么被折叠?



