如果计算机通过adsl上网的,那编程中如何获得adsl网络连接的ip地址?我现在只能得到本地局域网的ip地址.郁闷ing!
解决方案 »
- 中软 招人还是招生??
- 如何实现.txt结尾文件的搜索?
- 关于包和类路径变量等等的问题,希望高手帮帮我解答我的疑惑,谢谢~~~~~
- JFreeChar无法生成class文件
- 问个小问题
- 就这些分了,请问一台电脑要运行*.jar文件它至少要安装什么软件?谢谢 在线等
- 请问哪有JAVA的Rijndael算法加密包?用JAVA如何实现加密呢?
- 类中嵌套了一个private权限的接口,可以写这样的代码却不可以调用这个private接口的方法?为什么?郁闷!
- 怎么格式化输出数字??
- iText生成dbf,字太靠下了,怎样垂直居中?
- JEditorPanel里面怎么响应表单(按钮)啊
- 为什么用javascript的encodeURI编码跟用java.net.URLEncoder.encode(s)编码的结果不一样?
从结果中解析你需要的东西
二楼的兄弟能否说详细一点呢?
Process p=Runtime.getRuntime().exec("netstat -e");
得到一个netstat -e命令的子进程,用该进程的getInputstream将信息输出.
程序如下:
import java.io.InputStreamReader;
import java.io.BufferedReader;public class macaddress {
public static void main(String[] args) {
macaddress mdd = new macaddress();
String str=mdd.getMacOnWindow();
System.out.println(str);
}
private static String getMacOnWindow() {
String s = "";
try {
String s1 = "ipconfig /all";
Process process = Runtime.getRuntime().exec(s1);
BufferedReader bufferedreader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String nextLine;
for (String line = bufferedreader.readLine(); line != null; line = nextLine) {
nextLine = bufferedreader.readLine();
if (line.indexOf("Physical Address") <= 0) {
continue;
}
int i = line.indexOf("Physical Address") + 36;
s = line.substring(i);
break;
} bufferedreader.close();
process.waitFor();
} catch (Exception exception) {
s = "";
}
return s.trim();
}}
int ipNum; protected void analyzeDevices() {
deviceNum = 0;
ipNum = 0;
NetworkInterface tmpInterface;
String tmpStr;
Vector devicesVector = new Vector();
Vector ipNumVector = new Vector();
Vector ipAddressVector = new Vector();
Enumeration ipAddressesOnEachDevice;
Enumeration devices;
try {
devices = NetworkInterface.getNetworkInterfaces();
while (devices.hasMoreElements()) {
tmpInterface = (NetworkInterface) devices.nextElement();
tmpStr = tmpInterface.toString();
devicesVector.add(tmpStr.substring(5,
tmpStr.lastIndexOf("index:") - 1));
ipNum = 0;
ipAddressesOnEachDevice = tmpInterface.getInetAddresses();
while (ipAddressesOnEachDevice.hasMoreElements()) {
String ip = ipAddressesOnEachDevice.nextElement().toString();
ipAddressVector.add(ip);
ipNum++;
}
ipNumVector.add(new Integer(ipNum));
}
} catch (SocketException se) {
}
deviceNum = devicesVector.size();
deviceNames = new String[deviceNum];
ipNumOfEachDevice = new int[deviceNum];
for (int i = 0; i < deviceNum; i++) {
deviceNames[i] = (String) devicesVector.elementAt(i);
ipNumOfEachDevice[i] = ((Integer) ipNumVector.elementAt(i)).intValue();
}
ipNum = ipAddressVector.size();
ipAddresses = new String[ipNum];
for (int i = 0; i < ipAddresses.length; i++) {
ipAddresses[i] = (String) ipAddressVector.elementAt(i);
}
}
由于時間比較緊,我就不給你分析代碼了,你自己看看吧。
个人感觉前者确实只适合在windows下调用,而后者做到了平台无关性,
应该是一种更好的解决方案,我自己当时也查阅了森林兄用到的NetworkInterface
这个类,当时感觉应该是可以用它解决的,但是没有细看,现在看了代码,
感觉很激动,明天考试,考完就开始实验。感谢大家参与,期望更多的讨论!
if i use proxy ,by your method,i think i just can obtain the proxy ip!
do you think so?
但我使用了代理服务器的话那我不是只能得到代理服务器的地址了?你说呢?欢迎讨论!
刚考完试!哈哈!