java中讲讲URL的用法,举例?
2021/10/16 17:10:16
本文主要是介绍java中讲讲URL的用法,举例?,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
4.URL类的实例
马克-to-win:URL(Uniform Resource Locator-----一致资源查找器)它用来指向Internet上的资源文件,比如 http://java.sun.com:8080/docs/introdiction.htm net包中的URL类提供API来访问Internet上的信息。
比如以上的URL中:
1)协议:http
2)IP 地址或主机名:java.sun.com
3)端口号:8080
4)实际文件路径:docs/introdiction.htm
例:2.4.1
/*no need to have network through to run the program.*/
import java.net.*;
import java.io.*;
public class TestMark_to_win {
public static void main(String[] args) throws Exception {
/* Class URL represents a Uniform Resource Locator, a pointer to a
* "resource" on the World Wide Web. A resource can be something as
* simple as a file or a directory, public URL(String spec)throws
* MalformedURLException: Creates a URL object from the String
* representation.
*/
URL aURL = new URL("http://java.sun.com:8080/docs/books/"
+ "tutorial/index.html");
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("host = " + aURL.getHost());
System.out.println("filename = " + aURL.getFile());
System.out.println("default port = " + aURL.getDefaultPort());
System.out.println("port = " + aURL.getPort());
}
}
例:2.4.2
/* This program needs an open connection to run.
*/
import java.net.*;
import java.io.*;
import java.util.*;
public class TestMark_to_win {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("https://www.oracle.com/index.html");
/* public URLConnection openConnection() throws IOException: Returns a
* URLConnection object that represents a connection to the remote
* object referred to by the URL.
*/
URLConnection yahooConnection = yahoo.openConnection();
/* public long getLastModified() Returns the value of the last-modified
* header field. The result is the number of milliseconds since January
* 1, 1970 GMT.
*/
System.out.println("content LastModified"
+ new Date(yahooConnection.getLastModified()));
/*public InputStream getInputStream()throws IOException: Returns an
* input stream that reads from this open connection.
*/
// DataInputStream in = new
// DataInputStream(yahooConnection.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(
yahooConnection.getInputStream()));
String inputLine;
for (int i = 0; i < 25; i++) {
inputLine = in.readLine();
System.out.println(inputLine);
}
in.close();
}
}
更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44639795/article/details/102319021
这篇关于java中讲讲URL的用法,举例?的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-13怎么通过 JavaScript 或其他编程语言来实现监听屏幕高度变化功能?-icode9专业技术文章分享
- 2024-11-12聊聊我们那些年用过的表达式引擎组件
- 2024-11-12让项目数据更有说服力:五款必备数据可视化管理工具推荐
- 2024-11-12人到一定年纪,要学会远离多巴胺
- 2024-11-12解读:精益生产管理的目的是什么?如何操作?
- 2024-11-12Sku预研作业
- 2024-11-12文心一言API密钥:分步申请指南
- 2024-11-12初学者指南:轻松掌握后台交互
- 2024-11-12从零开始学习:封装基础知识详解
- 2024-11-12JSON对象入门教程:轻松掌握基础用法