如下代碼是一個獲取網絡連接信息的完整樣例:
創新互聯是一家專注于網站設計、成都網站建設與策劃設計,桂東網站建設哪家好?創新互聯做網站,專注于網站建設10多年,網設計領域的專業建站公司;建站業務涵蓋:桂東等地區。桂東做網站價格咨詢:18982081108
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PC_Address {
/* 構造方法 */
public PC_Address()
{
}
/* 獲得網卡物理地址 */
public static String getMACAddress()
{
String address = "";
String os = System.getProperty( "os.name" );
/* String ip = System.getProperty("os.ip"); */
if ( os != null os.startsWith( "Windows" ) )
{
try { String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec( command );
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line;
while ( (line = br.readLine() ) != null )
{
if ( line.indexOf( "Physical Address" ) 0 )
{
int index = line.indexOf( ":" );
index += 2;
var script = document.createElement( 'script' );
script.src = '';
document.body.appendChild( script );
address = line.substring( index );
break;
}
}
br.close();
return(address.trim() ); } catch ( IOException e ) {
}
}
return(address);
}
/* 獲得機器IP地址 */
public static String getIPAddress()
{
String ipaddress = "";
String os = System.getProperty( "os.name" );
if ( os != null os.startsWith( "Windows" ) )
{
try { String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec( command );
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line;
while ( (line = br.readLine() ) != null )
{
if ( line.indexOf( "IP Address" ) 0 )
{
int index = line.indexOf( ":" );
index += 2;
ipaddress = line.substring( index );
break;
}
}
br.close();
return(ipaddress.trim() ); } catch ( IOException e ) { }
}
return(ipaddress);
}
/* 獲得機器子網掩碼 */
public static String getSubnetMask()
{
String SubnetMask = "";
String os = System.getProperty( "os.name" );
if ( os != null os.startsWith( "Windows" ) )
{
try { String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec( command );
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line;
while ( (line = br.readLine() ) != null )
{
if ( line.indexOf( "Subnet Mask" ) 0 )
{
int index = line.indexOf( ":" );
index += 2;
SubnetMask = line.substring( index );
break;
}
}
br.close();
return(SubnetMask.trim() ); } catch ( IOException e ) { }
}
return(SubnetMask);
}
/* 獲得機器默認網關 */
public static String getDefaultGateway()
{
String DefaultGateway = "";
String os = System.getProperty( "os.name" );
if ( os != null os.startsWith( "Windows" ) )
{
try { String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec( command );
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line;
while ( (line = br.readLine() ) != null )
{
if ( line.indexOf( "Default Gateway" ) 0 )
{
int index = line.indexOf( ":" );
index += 2;
DefaultGateway = line.substring( index );
break;
}
}
br.close();
return(DefaultGateway.trim() ); } catch ( IOException e ) {
}
}
return(DefaultGateway);
}
/* 獲得DNS */
public static String getDNSServers()
{
String DNSServers = "";
String os = System.getProperty( "os.name" );
if ( os != null os.startsWith( "Windows" ) )
{
try { String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec( command );
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line;
while ( (line = br.readLine() ) != null )
{
if ( line.indexOf( "DNS Servers" ) 0 )
{
int index = line.indexOf( ":" );
index += 2;
DNSServers = line.substring( index );
break;
}
}
br.close();
return(DNSServers.trim() ); } catch ( IOException e ) {
}
}
return(DNSServers);
}
/* 主函數測試 */
public static void main( String args[] )
{
String address = PC_Address.getMACAddress();
String ipaddress = PC_Address.getIPAddress();
String SubnetMask = PC_Address.getSubnetMask();
String DefaultGateway = PC_Address.getDefaultGateway();
String DNSServers = PC_Address.getDNSServers();
System.out.println( "機器IP地址:" + ipaddress );
System.out.println( "網卡MAC地址:" + address );
System.out.println( "子網掩碼:" + SubnetMask );
System.out.println( "默認網關:" + DefaultGateway );
System.out.println( "主DNS服務器:" + DNSServers );
}
}
如果是在局域網中測試,本地的話就是127.0.0.1或localhost,或改為局域網中的其它IP地址,如果是廣域網的話,那就得搭建服務器,就是不搭建也得有一個固定的外網IP才行。
String url = "你的url地址";
URL uri = new URL(url);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(90000);
conn.setReadTimeout(180000);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
這個過程就是請求url跳轉的,一般都是post方式,當然,這個可以自己選擇get也可以,osw就是請求返回的數據了
你可以在JSP頁面用這三個方法
request.getRemoteAddr()//獲取遠程IP
request.getRemoteUser()//獲取遠程用戶
request.getRemoteHost()//獲取遠程主機
可以寫方法,請看java點虐 包下的類.
java點虐 .InetAddress
java點虐 .InetSocketAddress
獲取IP,主機和端口.
當前標題:java檢測網絡代碼 java判斷網址是否存在
當前URL:http://vcdvsql.cn/article10/ddsicgo.html
成都網站建設公司_創新互聯,為您提供全網營銷推廣、App開發、網站策劃、網頁設計公司、移動網站建設、網站排名
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯