For the time being, when I googled, this page came out. http://www.yukun.info/blog/2008/10/java-inetaddress-getlocalhost.html
It is the result of copying the circle and running it as it is. An IP address that I didn't recognize was output.
If you tilt your head, launch a command prompt and ipconfig.
"Who are you!"
I don't know what happens in other environments, but at least on this machine I found that using InetAddress.getLocalHost () doesn't give me a Wi-Fi IP. Even if you keep googled, only Android articles will hit, so it seems that you have to think of another method even if you push it.
Since you can see it at the command prompt, I thought that I should be able to mess with this on the Java side, so I decided to try it. I googled that Java should also have System () in C language.
http://d.hatena.ne.jp/takami_hiroki/20101221/p1
Based on this, if you get the output by the OS command ipconfig and display it ...
Yes it was. The character code used in the Japanese version of Windows command prompt is Shift-JIS. I tried to make the output and the character string after acquisition into UTF-8, but it didn't come true whether I was a small fish or a specification.
As I investigated, it turned out that "text files written in Shift-JIS can be read". You can use the text file generated by executing "ipconfig> ip.txt". As a flow
ipconfig > ip.txt ↓ Specify the character code as Shift-JIS and read ip.txt ↓ Search using indexOf () or split () ↓ Delete ip.txt with a face that doesn't eat anything
What a place, such as.
It should be noted here that the main body side that hits the OS command and the write side that returns the result when hit are parallel processing. If you start reading without waiting for the process on the writing side to finish, the IOException teacher will naturally come.
Main.java
public class Main {
public static void main(String[] args) {
System.out.println(new getWiFiIP().get());
}
}
getWiFiIP.java
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class getWiFiIP {
private String ip="";
public String get(){
return ip;
}
public getWiFiIP() {
try {
ProcessBuilder pb=new ProcessBuilder("cmd.exe", "/c", "ipconfig",">","ip.txt");
Process process = pb.start(); //Start the process
process.waitFor(); //Wait until the export is finished
ArrayList<String> list=new Text_read("ip.txt").getText(); //Read text file
int cnt=list.indexOf("Wireless LAN adapter Wi-Fi:");////////////////////////////////////////////////
for(;;){
if(list.get(cnt).indexOf("IPv4")==-1){
cnt++;
}else{
break;
} //Wi from the character string group read and acquired-IP address of Fi connection(IPv4)To search for
}////////////////////////////////////////////////////////////////////////////////////////////////////
String[] temp=list.get(cnt).split(":"); //"IPv4 address. . . . . . . . . . . .: ×××.×××.×××.Since it is "XXX", divide it
ip= temp[1]; //Substitute the acquired IP address
new File("ip.txt").delete(); //IP.Delete txt
////////////////////////////////////////////////////The following error handling
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(ip.equals(""))ip= "Failed to get";
}
}
Text_read.java
import java.io.*;
import java.util.ArrayList;
/////////////////////////////////////////////////////////
//Read the entire text file and store it in ArrayList.//
/////////////////////////////////////////////////////////
public class Text_read {
private ArrayList<String>text=new ArrayList<String>(); //ArrayList for storage
public Text_read(String path/*File Path*/){
try {
BufferedReader br =new BufferedReader(new InputStreamReader(new FileInputStream(path), "Shift_JIS"));
for(;;){
text.add(br.readLine());
if(text.get(text.size()-1)==null){
text.remove(text.size()-1);
break;
}
}
br.close();
}catch(IOException e){
System.out.println("I / O error");
}
}
public ArrayList<String> getText(){
return text;
}
}
This solved it for the time being, but if anyone knows an easier way, please let me know ...
Recommended Posts