网上那些例子 试了本来都不会 都不知从何下手
不用cmdconfig 利用 获取doc 下的那些都写 rec(ipconfig)不用这个
我这win7 和 xp 都不一样
谢谢了 苦等
interfaceaddress 这个泪下的那个方法呀
------解决方案--------------------
- Java code
public static void main(String[] args) throws IOException { String os = System.getProperties().getProperty("os.name"); //得到操作系统 xp 为"Windows XP" 其他的的楼主自己去试试 Process pro = Runtime.getRuntime().exec("ipconfig"); BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream())); List<String> rowList = new ArrayList(); String temp; while((temp = br.readLine()) != null){ rowList.add(temp ); } for (String string : rowList) { String sm= os.equals("Windows XP") ? "Subnet Mask" : "子网掩码" ; //这里只判断了win7个xp if(string.indexOf("Subnet Mask") != -1){ Matcher mc = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(string); if(mc.find()){ System.out.println("子掩码:" + mc.group()); }else{ System.out.println("子掩码为空"); } }; String dg = os.equals("Windows XP") ? "Default Gateway" : "默认网关" ; //这里只判断了win7个xp if(string.indexOf("Default Gateway") != -1){ Matcher mc = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(string); if(mc.find()){ System.out.println("默认网关:" + mc.group()); }else{ System.out.println("默认网关为空"); } return; }; } }
------解决方案--------------------
------解决方案--------------------
使用java.lang.Process类啊
将"ipconfig /all"传进去
- Java code
/** * 获取linuxShell命令结果. * @param cmd 指令 * @return 指令结果 */ public String getRuntimeExec(final String cmd) { BufferedReader bufferedReader = null; Process process = null; StringBuffer sb = new StringBuffer(); try { String buffer; //System.out.println(cmd); process = Runtime.getRuntime().exec(cmd); // 得到结果 bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); while ((buffer = bufferedReader.readLine()) != null) { sb.append(buffer + "\n"); } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("execute failed."); return null; } finally { // 清理 try { if (bufferedReader != null) { bufferedReader.close(); bufferedReader = null; } if (process != null) { process.destroy(); process = null; } } catch (Exception e) { e.printStackTrace(); } } }
------解决方案--------------------
也来弄一段:,只能得到掩码
- Java code
public static void main(String args[]) { try { for (Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); nis != null && nis.hasMoreElements();) { NetworkInterface ni = nis.nextElement(); System.out.println("网络适配器:" + ni.getDisplayName()); for (InterfaceAddress ifAddr : ni.getInterfaceAddresses()) { System.out.println("IP:" + ifAddr.getAddress().getHostAddress()); System.out.println("Mask:" + getMask(ifAddr.getNetworkPrefixLength()) ); //System.out.println("Borad:"+ ifAddr.getBroadcast().getHostAddress()); } } } catch (Exception e) { e.printStackTrace(); } } /** * 返回指定长度的掩码的字符串,如,长度为8的掩码,返回255.0.0.0;长度为24的掩码,返回255.255.255.0<br/> * 注:只适用于IPV4 * @param maskLength 掩码长度 * @return */ private static String getMask(int maskLength){ StringBuffer maskStr = new StringBuffer(); int mask = 0xFFFFFFFF << 32 - maskLength ; for(int i = 3 ;i >= 0;i--){ maskStr.append( ( mask >> (8*i) ) & 0xFF); if(i>0){ maskStr.append("."); } } return maskStr.toString(); }