import com.huluwa.cc.ipconfig.DataBlock;
    import com.huluwa.cc.ipconfig.DbConfig;
    import com.huluwa.cc.ipconfig.DbSearcher;
    import com.huluwa.cc.ipconfig.Util;
    import org.apache.commons.io.FileUtils;

    import javax.servlet.http.HttpServletRequest;

    import java.io.File;
    import java.io.IOException;
    import java.lang.reflect.Method;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.util.Objects;

    public class IpUtil {

    1. _/**_<br />_ * 获取合法ip地址_<br />_ * _**_@param _**_request_<br />_ * _**_@return_**<br />**_ _**_*/_<br />_ _public static String getRealIpAddress(HttpServletRequest request) {<br /> String ip = request.getHeader("x-forwarded-for");<br /> if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {<br /> ip = request.getHeader("Proxy-Client-IP");<br /> }<br /> if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {<br /> ip = request.getHeader("WL-Proxy-Client-IP");<br /> }<br /> if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {<br /> ip = request.getRemoteAddr();<br /> if(ip.equals("127.0.0.1")){<br /> //根据网卡取本机配置的IP<br /> InetAddress inet=null;<br /> try {<br /> inet = InetAddress._getLocalHost_();<br /> } catch (UnknownHostException e) {<br /> e.printStackTrace();<br /> }<br /> ip= inet.getHostAddress();<br /> }<br /> }<br /> // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割<br /> if(ip != null && ip.length() > 15){<br /> if(ip.indexOf(",")>0){<br /> ip = ip.substring(0,ip.indexOf(","));<br /> }<br /> }<br /> return ip;<br /> }
    2. public static String getCityInfo(String ip){
    3. String dbPath = IpUtil.class.getResource("/ip2region/ip2region.db").getPath();<br /> File file = new File(dbPath);<br /> if (!file.exists()) {<br /> String tmpDir = System._getProperties_().getProperty("java.io.tmpdir");<br /> dbPath = tmpDir + "ip.db";<br /> file = new File(dbPath);<br /> try {<br /> FileUtils._copyInputStreamToFile_(Objects._requireNonNull_(IpUtil.class.getClassLoader().getResourceAsStream("classpath:ip2region/ip2region.db")), file);<br /> } catch (IOException e) {<br /> e.printStackTrace();<br /> }<br /> }
    4. //查询算法<br /> int algorithm = DbSearcher._BTREE_ALGORITHM_; //B-tree<br /> //DbSearcher.BINARY_ALGORITHM //Binary<br /> //DbSearcher.MEMORY_ALGORITYM //Memory<br /> try {<br /> DbConfig config = new DbConfig();<br /> DbSearcher searcher = new DbSearcher(config, dbPath);
    5. //define the method<br /> Method method = null;<br /> switch ( algorithm )<br /> {<br /> case DbSearcher._BTREE_ALGORITHM_:<br /> method = searcher.getClass().getMethod("btreeSearch", String.class);<br /> break;<br /> case DbSearcher._BINARY_ALGORITHM_:<br /> method = searcher.getClass().getMethod("binarySearch", String.class);<br /> break;<br /> case DbSearcher._MEMORY_ALGORITYM_:<br /> method = searcher.getClass().getMethod("memorySearch", String.class);<br /> break;<br /> }
    6. DataBlock dataBlock = null;<br /> if ( Util._isIpAddress_(ip) == false ) {<br /> System._out_.println("Error: Invalid ip address");<br /> }
    7. dataBlock = (DataBlock) method.invoke(searcher, ip);
    8. return dataBlock.getRegion();
    9. } catch (Exception e) {<br /> e.printStackTrace();<br /> }
    10. return null;<br /> }
    11. public static void main(String[] args) throws Exception{<br /> System._err_.println(_getCityInfo_("171.11.100.5"));<br /> }

    }