java 開發(fā)中網(wǎng)絡(luò)編程之IP、URL詳解及實(shí)例代碼
java 網(wǎng)絡(luò)編程
java.net
類 InetAddress 此類表示互聯(lián)網(wǎng)協(xié)議 (IP) 地址。 會(huì)拋出異常 UnknownHostException
直接已知子類:
Inet4Address, Inet6Address
沒有構(gòu)造函數(shù),但是可以通過靜態(tài)方法獲取對象后,在完成其它功能的使用。
例如:
static InetAddress getLocalHost() 返回本地主機(jī)。 static InetAddress getByName(String host) 在給定主機(jī)名的情況下確定主機(jī)的 IP 地址。 static InetAddress[] getAllByName(String host) 在給定主機(jī)名的情況下,根據(jù)系統(tǒng)上配置的名稱服務(wù)返回其 IP 地址所組成的數(shù)組。 String getHostAddress() 返回 IP 地址字符串(以文本表現(xiàn)形式)。 String getHostName() 獲取此 IP 地址的主機(jī)名 String getCanonicalHostName() 獲取此 IP 地址的完全限定域名。即將主機(jī)名解析為IP地址
例子1:
import java.net.*;
class IPDemo
{
public static void main(String[] args) throws Exception
{
// InetAddress localhost = InetAddress.getLocalHost();
// System.out.println("localhost="+localhost); //返回本地主機(jī)(主機(jī)名和IP地址)
// String hostname = localhost.getHostName(); //返回本地主機(jī)中的主機(jī)名
// String hostIP = localhost.getHostAddress(); //返回本地主機(jī)中的IP地址
// System.out.println("hostname="+hostname+"\n"+"hostIP="+hostIP);
//InetAddress ia = InetAddress.getByName("www.baidu.com");
//System.out.println("name="+ia.getHostName());
//System.out.println("adress="+ia.getHostAddress());
InetAddress[] iad = InetAddress.getAllByName("www.baidu.com");//百度提供的不止一個(gè)主機(jī)
for(int i=0;i<iad.length;i++)
{
System.out.println("name="+iad[i].getHostName());
System.out.println("adress="+iad[i].getHostAddress());
}
}
}
import java.net.*; String getFile() 獲取此 URL 的文件名。 String getHost() 獲取此 URL 的主機(jī)名(如果適用)。 String getPath() 獲取此 URL 的路徑部分。 int getPort() 獲取此 URL 的端口號。 String getProtocol() 獲取此 URL 的協(xié)議名稱。 String getQuery() 獲取此 URL 的查詢部分。
例子2:URL使用
class URLDemo
{
public static void main(String[] args)throws Exception
{
URL url = new URL("http://192.168.1.105:8080/myweb/demo.html?name=haha&age=20");
System.out.println("getProtocol() :"+url.getProtocol());
System.out.println("getHost() :"+url.getHost());
System.out.println("getPort() :"+url.getPort());
System.out.println("getFile() :"+url.getFile());
System.out.println("getPath() :"+url.getPath());
System.out.println("getQuery() :"+url.getQuery());
}
}
例子3:URLConnection連接
import java.io.*;
import java.net.*;
class URLConnectionDemo
{
public static void main(String[] args)throws Exception
{
URL url = new URL("http://192.168.1.105:8080/myweb/demo.html");
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Java的Socket網(wǎng)絡(luò)編程基礎(chǔ)知識入門教程
- java必學(xué)必會(huì)之網(wǎng)絡(luò)編程
- Java套接字(Socket)網(wǎng)絡(luò)編程入門
- 簡單介紹Java網(wǎng)絡(luò)編程中的HTTP請求
- Java網(wǎng)絡(luò)編程之簡單的服務(wù)端客戶端應(yīng)用實(shí)例
- Java網(wǎng)絡(luò)編程基礎(chǔ)教程之Socket入門實(shí)例
- java網(wǎng)絡(luò)編程之socket網(wǎng)絡(luò)編程示例(服務(wù)器端/客戶端)
- java網(wǎng)絡(luò)編程之識別示例 獲取主機(jī)網(wǎng)絡(luò)接口列表
- java網(wǎng)絡(luò)編程學(xué)習(xí)java聊天程序代碼分享
- java網(wǎng)絡(luò)編程中向指定URL發(fā)送GET POST請求示例
相關(guān)文章
MybatisPlus使用@TableLogic實(shí)現(xiàn)邏輯刪除過程
這篇文章主要介紹了MybatisPlus使用@TableLogic實(shí)現(xiàn)邏輯刪除過程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
SpringBoot整合jnotify實(shí)現(xiàn)針對指定目錄及其(動(dòng)態(tài))子目錄的監(jiān)聽的方法
本文介紹了JNotify這一Java庫在SpringBoot中的應(yīng)用,JNotify允許應(yīng)用程序監(jiān)聽文件系統(tǒng)事件,包括文件夾/文件的創(chuàng)建、刪除、修改和重命名,由于JNotify底層調(diào)用的關(guān)鍵部分是C語言開發(fā)的,所以在使用前需要在系統(tǒng)中加入相應(yīng)的動(dòng)態(tài)庫2024-10-10
Java向上轉(zhuǎn)型與向下轉(zhuǎn)型超詳細(xì)圖解
Java實(shí)現(xiàn)多個(gè)wav文件合成一個(gè)的方法示例
Java的Spring框架下RMI與quartz的調(diào)用方法
通過java備份恢復(fù)mysql數(shù)據(jù)庫的實(shí)現(xiàn)代碼

