자바 IP 에 따라 사용자 소재 지 를 가 져 옵 니 다.
 
 import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
public class AddressUtils {
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		AddressUtils addressUtils = new AddressUtils();
		
		  String ip = "118.213.176.78";
		  
		  String address = "";
		  
		  try {
			  
		   address = addressUtils.getAddress("ip="+ip, "utf-8");
		   
		  } catch (Exception e) {
			  
			  e.printStackTrace();
		  }
		  
		  System.out.println(address);
		  
	}
	/**
	 *     
	 * @param params
	 * @param encoding
	 * @return
	 * @throws Exception
	 */
	public String getAddress(String params, String encoding) throws Exception{
		
		String path = "http://ip.taobao.com/service/getIpInfo.php";
		
		String returnStr = this.getRs(path, params, encoding);
		
		JSONObject json=null;
		
		if(returnStr != null){
			
			json = new JSONObject(returnStr);
			
			if("0".equals(json.get("code").toString())){
				
				StringBuffer buffer = new StringBuffer();
				
				//buffer.append(decodeUnicode(json.optJSONObject("data").getString("country")));//  
				
				//buffer.append(decodeUnicode(json.optJSONObject("data").getString("area")));//  
				
				buffer.append(decodeUnicode(json.optJSONObject("data").getString("region")));//  
				
				buffer.append(decodeUnicode(json.optJSONObject("data").getString("city")));//  
				
				buffer.append(decodeUnicode(json.optJSONObject("data").getString("county")));//  
				
				buffer.append(decodeUnicode(json.optJSONObject("data").getString("isp")));//ISP  
				
				System.out.println(buffer.toString());
				
				return buffer.toString();
				
			}else{
				
				return "      �?";
				
			}
			
		}
		
		return null;
		
	}
	/**
	 *  url    
	 * @param path
	 * @param params
	 * @param encoding
	 * @return
	 */
	public String getRs(String path, String params, String encoding){
		
		URL url = null;
		
		HttpURLConnection connection = null;
			
		try {
			
			url = new URL(path);
				
			connection = (HttpURLConnection)url.openConnection();//       
				
			connection.setConnectTimeout(2000);//         ,   �?
			
			connection.setReadTimeout(2000);//           ,   �?
			
			connection.setDoInput(true);//       �? true|false
			
			connection.setDoOutput(true);//        true|false
			
			connection.setRequestMethod("POST");//     POST|GET
			
			connection.setUseCaches(false);//     true|false
			
			connection.connect();//       
			
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());
			
			out.writeBytes(params);
			
			out.flush();
			
			out.close();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),encoding));
			
			StringBuffer buffer = new StringBuffer();
			
			String line = "";
			
			while ((line = reader.readLine())!= null) {
				
				buffer.append(line);
				
			}
			
			reader.close();
			
			return buffer.toString();
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}finally{
			
			connection.disconnect();//     
			
		}
		
		return null;
	}
	/**
	 *     
	 * @param theString
	 * @return
	 */
	public static String decodeUnicode(String theString){
		
		char aChar;
		
		int len = theString.length();
		
		StringBuffer buffer = new StringBuffer(len);
		
		for (int i = 0; i < len;) {
			
			aChar = theString.charAt(i++);
			
			if(aChar == '\\'){
				
				aChar = theString.charAt(i++);
			
				if(aChar == 'u'){
					
					int val = 0;
					
					for(int j = 0; j < 4; j++){
						
						aChar = theString.charAt(i++);
						
						switch (aChar) {
						
						case '0':
							
						case '1':
							
						case '2':
							
						case '3':
							
						case '4':
						
						case '5':
							
						case '6':
						
						case '7':
							
						case '8':
							
						case '9':
							
						val = (val << 4) + aChar - '0';
						
						break;
	
						case 'a':
							
						case 'b':
							
						case 'c':
							
						case 'd':
							
						case 'e':
							
						case 'f':
							
						val = (val << 4) + 10 + aChar - 'a';
						       
						break;
						
						case 'A':
							
						case 'B':
							
						case 'C':
							
						case 'D':
							
						case 'E':
							
						case 'F':
						  
						val = (val << 4) + 10 + aChar - 'A';
						       
						break;
						       
						default:
						
						throw new IllegalArgumentException(
					         
							"Malformed      encoding.");
					    }
						
					}
					
					buffer.append((char) val);
					
					}else{
						
						if(aChar == 't'){
							
							aChar = '\t';
						}
						
						if(aChar == 'r'){
							
							aChar = '\r';
						}
						
						if(aChar == 'n'){
							
							aChar = '
';
						}
						
						if(aChar == 'f'){
							
							aChar = '\f';
							
						}
						
						buffer.append(aChar);
					}
				
				}else{
					
					buffer.append(aChar);
					
				}
				
			}
		
		return buffer.toString();
		
	}
}
  
참조 주소
jar 패키지 다운로드
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.