<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/post//</link>
<title><![CDATA[Java判断用户ip是否在指定IP段范围内]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[编程杂谈]]></category>
<pubDate>Mon, 08 Feb 2021 02:00:55 +0000</pubDate> 
<guid>https://www.heckjj.com/post//</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp;/*验证IP是否属于某个IP段<br/>&nbsp;&nbsp; *<br/>&nbsp;&nbsp; * ipSection&nbsp;&nbsp;&nbsp;&nbsp;IP段（以&#039;-&#039;分隔）<br/>&nbsp;&nbsp; * ip&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 所验证的IP号码<br/>&nbsp;&nbsp; *<br/>&nbsp;&nbsp; */<br/>&nbsp;&nbsp;public static boolean ipExistsInRange(String ip, String ipSection) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ipSection = ipSection.trim();<br/>&nbsp;&nbsp;&nbsp;&nbsp;ip = ip.trim();<br/>&nbsp;&nbsp;&nbsp;&nbsp;int idx = ipSection.indexOf(&#039;-&#039;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;String beginIP = ipSection.substring(0, idx);<br/>&nbsp;&nbsp;&nbsp;&nbsp;String endIP = ipSection.substring(idx + 1);<br/>&nbsp;&nbsp;&nbsp;&nbsp;return getIp2long(beginIP) &lt;= getIp2long(ip) &amp;&amp; getIp2long(ip) &lt;= getIp2long(endIP);<br/>&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;public static long getIp2long(String ip) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ip = ip.trim();<br/>&nbsp;&nbsp;&nbsp;&nbsp;String[] ips = ip.split(&quot;&#92;&#92;.&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;long ip2long = 0L;<br/>&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; 4; ++i) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip2long = ip2long &lt;&lt; 8 &#124; Integer.parseInt(ips[i]);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;return ip2long;<br/>&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;public static long getIp2long2(String ip) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ip = ip.trim();<br/>&nbsp;&nbsp;&nbsp;&nbsp;String[] ips = ip.split(&quot;&#92;&#92;.&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;long ip1 = Integer.parseInt(ips[0]);<br/>&nbsp;&nbsp;&nbsp;&nbsp;long ip2 = Integer.parseInt(ips[1]);<br/>&nbsp;&nbsp;&nbsp;&nbsp;long ip3 = Integer.parseInt(ips[2]);<br/>&nbsp;&nbsp;&nbsp;&nbsp;long ip4 = Integer.parseInt(ips[3]);<br/>&nbsp;&nbsp;&nbsp;&nbsp;long ip2long = 1L * ip1 * 256 * 256 * 256 + ip2 * 256 * 256 + ip3 * 256 + ip4;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;return ip2long;<br/>&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;public static boolean isIntranetIP(String ip) &#123;<br/>&nbsp;&nbsp;ipExistsInRange(ip, &quot;172.16.0.0-172.31.255.255&quot;) &#124;&#124; ip.startsWith(&quot;10.&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//10.10.10.116 是否属于固定格式的IP段10.10.1.00-10.10.255.255<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String ip=&quot;10.10.10.116&quot;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String ipSection=&quot;10.10.1.00-10.10.255.255&quot;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;boolean exists=ipExistsInRange(ip,ipSection);<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(exists);<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(getIp2long(ip));<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(getIp2long2(ip));<br/>&nbsp;&nbsp;&#125;
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post//#blogcomment</link>
<title><![CDATA[[评论] Java判断用户ip是否在指定IP段范围内]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>