java检测网络代码 java判断网址是否存在
java 如何获取连接网络连接信息
如下代码是一个获取网络连接信息的完整样例:
创新互联是一家专注于网站设计、成都网站建设与策划设计,桂东网站建设哪家好?创新互联做网站,专注于网站建设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 );
}
}
Java的网络连接代码如何测试?
如果是在局域网中测试,本地的话就是127.0.0.1或localhost,或改为局域网中的其它IP地址,如果是广域网的话,那就得搭建服务器,就是不搭建也得有一个固定的外网IP才行。
Java 测试URL地址是否能正常连接的代码
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就是请求返回的数据了
java如何实现监控服务器网络流量
你可以在JSP页面用这三个方法
request.getRemoteAddr()//获取远程IP
request.getRemoteUser()//获取远程用户
request.getRemoteHost()//获取远程主机
可以写方法,请看java点虐 包下的类.
java点虐 .InetAddress
java点虐 .InetSocketAddress
获取IP,主机和端口.
标题名称:java检测网络代码 java判断网址是否存在
URL分享:http://ybzwz.com/article/ddsicgo.html