- 浏览: 675873 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (254)
- java分布式应用架构 (22)
- SSH框架整合 (6)
- java web 学习笔记 (49)
- java 学习笔记 (56)
- struts 2 学习 (6)
- Hibernate学习 (10)
- spring 学习 (2)
- 客户端编程(javascript) (4)
- IDE使用 (13)
- 生命 人生 (6)
- 系统维护 (3)
- 技术篇 (10)
- MySql (2)
- J2ME (1)
- java网络编程 (4)
- 数据库 (5)
- C/C++ (8)
- Oracle (7)
- 软件测试 (0)
- 软件的安装和部署 (0)
- Java快讯 (1)
- swt (1)
- Flex (1)
- 软件工程 (1)
- PostgreSQL (1)
- sql server2000 (2)
- 嵌入式数据库sqlite (5)
- J2EE (1)
- XML (1)
- ibatis3(MyBatis) (6)
- Linux&Unix (1)
- velocity (1)
- 回报社会 (4)
- 软件项目管理 (3)
- android研究 (3)
- C# (2)
- Objective-C (1)
- 音乐 (0)
- webx (1)
- JMS (1)
- maven软件项目管理 (1)
- 分布式服务 (0)
- 云平台 (0)
- 分布式存储 (1)
- 分布式系统架构 (0)
- 移动互联网 (1)
- ZooKeeper (1)
最新评论
-
liyys:
楼主,可不可以发这个项目的源码工程出来分享一下,少了几个类。楼 ...
仿照Hibernate实现一个SQLite的ORM框架 -
liyys:
少了一些类的源码没有粘贴出来
仿照Hibernate实现一个SQLite的ORM框架 -
honglei0412:
我使用的是这种方式获取db文件的目录但是 URL p = Fi ...
使用sqlite注意事项 -
honglei0412:
大侠 能不能说明下DbFile您是怎么做的吗?
使用sqlite注意事项 -
ahack:
刚写完mapping才发现早就有人写好了。仔细一看还都是针对的 ...
仿照Hibernate实现一个SQLite的ORM框架
public String getIpAddrByRequest(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; }
C#根据用户IP地址查询用户信息
调用接口来自sina,显示的信息较为全面。
接口地址为 http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={ip}
返回json,格式为如下
{"ret":1,"start":"59.174.77.0","end":"59.174.80.156","country":"\u4e2d\u56fd",
"province":"\u6e56\u5317","city":"\u6b66\u6c49","district":"","isp":"\u7535\u4fe1","type":"","desc":""}
为了方便使用,加入类IpDetai解析json。
使用方式(ASP.NET):
Label1.Text = IpHelper.Get("xx.xx.xx.xxx",null).Province;
另外JSON解析用到了.net 3.5的System.Web.Script.Serialization.JavaScriptSerializer
using System; using System.IO; using System.Net; using System.Text; using System.Web.Script.Serialization; namespace IpUtils { public class IpDetail { public String Ret { get; set; } public String Start { get; set; } public String End { get; set; } public String Country { get; set; } public String Province { get; set; } public String City { get; set; } public String District { get; set; } public String Isp { get; set; } public String Type { get; set; } public String Desc { get; set; } } public class IpHelper { /// <summary> /// 获取IP地址的详细信息,调用的借口为 /// http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={ip} /// </summary> /// <param name="ipAddress">请求分析得IP地址</param> /// <param name="sourceEncoding">服务器返回的编码类型</param> /// <returns>IpUtils.IpDetail</returns> public static IpDetail Get(String ipAddress,Encoding sourceEncoding) { String ip = string.Empty; if(sourceEncoding==null) sourceEncoding = Encoding.UTF8; using (var receiveStream = WebRequest.Create("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip="+ipAddress).GetResponse().GetResponseStream()) { using (var sr = new StreamReader(receiveStream, sourceEncoding)) { var readbuffer = new char[256]; int n = sr.Read(readbuffer, 0, readbuffer.Length); int realLen = 0; while (n > 0) { realLen = n; n = sr.Read(readbuffer, 0, readbuffer.Length); } ip = sourceEncoding.GetString(sourceEncoding.GetBytes(readbuffer, 0, realLen)); } } return !string.IsNullOrEmpty(ip)?new JavaScriptSerializer().Deserialize<IpDetail>(ip):null; } } public class EncodingHelper { public static String GetString(Encoding source, Encoding dest, String soureStr) { return dest.GetString(Encoding.Convert(source, dest, source.GetBytes(soureStr))); } public static String GetString(Encoding source, Encoding dest, Char[] soureCharArr, int offset, int len) { return dest.GetString(Encoding.Convert(source, dest, source.GetBytes(soureCharArr, offset, len))); } } }
获取 IP 地址对应的地区(JavaScript)
function detect_city($ip) { $default = 'Hollywood, CA'; if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost') $ip = '8.8.8.8'; $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)'; $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip); $ch = curl_init(); $curl_opt = array( CURLOPT_FOLLOWLOCATION => 1, CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_USERAGENT => $curlopt_useragent, CURLOPT_URL => $url, CURLOPT_TIMEOUT => 1, CURLOPT_REFERER => 'http://' . $_SERVER['HTTP_HOST'], ); curl_setopt_array($ch, $curl_opt); $content = curl_exec($ch); if (!is_null($curl_info)) { $curl_info = curl_getinfo($ch); } curl_close($ch); if ( preg_match('{<li>City : ([^<]*)</li>}i', $content, $regs) ) { $city = $regs[1]; } if ( preg_match('{<li>State/Province : ([^<]*)</li>}i', $content, $regs) ) { $state = $regs[1]; } if( $city!='' && $state!='' ){ $location = $city . ', ' . $state; return $location; }else{ return $default; } }
Android 获取 IP 地址
public String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e(LOG_TAG, ex.toString()); } return null; }
分析用户ip归属地
<?php //这些是核心部分,可以写到一个单独的php里,用的时候直接include就可以 define('__QQWRY__' , dirname(__FILE__)."/qqwry.dat"); class QQWry{ var $StartIP=0; var $EndIP=0; var $Country=''; var $Local=''; var $CountryFlag=0; var $fp; var $FirstStartIp=0; var $LastStartIp=0; var $EndIpOff=0 ; function getStartIp($RecNo){ $offset=$this->FirstStartIp+$RecNo * 7 ; @fseek($this->fp,$offset,SEEK_SET) ; $buf=fread($this->fp ,7) ; $this->EndIpOff=ord($buf[4]) + (ord($buf[5])*256) + (ord($buf[6])* 256*256); $this->StartIp=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256); return $this->StartIp; } function getEndIp(){ @fseek ( $this->fp , $this->EndIpOff , SEEK_SET ) ; $buf=fread ( $this->fp , 5 ) ; $this->EndIp=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256); $this->CountryFlag=ord ( $buf[4] ) ; return $this->EndIp ; } function getCountry(){ switch ( $this->CountryFlag ) { case 1: case 2: $this->Country=$this->getFlagStr ( $this->EndIpOff+4) ; //echo sprintf('EndIpOffset=(%x)',$this->EndIpOff ); $this->Local=( 1 == $this->CountryFlag )? '' : $this->getFlagStr ( $this->EndIpOff+8); break ; default : $this->Country=$this->getFlagStr ($this->EndIpOff+4) ; $this->Local=$this->getFlagStr ( ftell ( $this->fp )) ; } } function getFlagStr ($offset){ $flag=0 ; while(1){ @fseek($this->fp ,$offset,SEEK_SET) ; $flag=ord(fgetc($this->fp ) ) ; if ( $flag == 1 || $flag == 2 ) { $buf=fread ($this->fp , 3 ) ; if ($flag==2){ $this->CountryFlag=2; $this->EndIpOff=$offset - 4 ; } $offset=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])* 256*256); } else{ break ; } } if($offset<12) return ''; @fseek($this->fp , $offset , SEEK_SET ) ; return $this->getStr(); } function getStr ( ) { $str='' ; while ( 1 ) { $c=fgetc ( $this->fp ) ; if(ord($c[0])== 0 ) break ; $str.= $c ; } return $str ; } function qqwry ($dotip='') { if( !is_string($dotip) || $dotip==''){return;} if(preg_match("/^127/",$dotip)){$this->Country="本地网络";return ;} elseif(preg_match("/^192/",$dotip)) {$this->Country="局域网";return ;} $nRet; $ip=$this->IpToInt ( $dotip ); $this->fp= fopen(__QQWRY__, "rb"); if ($this->fp == NULL) { $szLocal= "OpenFileError"; return 1; } @fseek ( $this->fp , 0 , SEEK_SET ) ; $buf=fread ( $this->fp , 8 ) ; $this->FirstStartIp=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256); $this->LastStartIp=ord($buf[4]) + (ord($buf[5])*256) + (ord($buf[6])*256*256) + (ord($buf[7])*256*256*256); $RecordCount= floor( ( $this->LastStartIp - $this->FirstStartIp ) / 7); if ($RecordCount <= 1){ $this->Country="FileDataError"; fclose($this->fp) ; return 2 ; } $RangB= 0; $RangE= $RecordCount; while ($RangB < $RangE-1) { $RecNo= floor(($RangB + $RangE) / 2); $this->getStartIp ( $RecNo ) ; if ( $ip == $this->StartIp ) { $RangB=$RecNo ; break ; } if ($ip>$this->StartIp) $RangB= $RecNo; else $RangE= $RecNo; } $this->getStartIp ( $RangB ) ; $this->getEndIp ( ) ; if ( ( $this->StartIp <= $ip ) && ( $this->EndIp >= $ip ) ){ $nRet=0 ; $this->getCountry ( ) ; $this->Local=str_replace("(我们一定要解放台湾!!!)", "", $this->Local); } else{ $nRet=3 ; $this->Country='未知' ; $this->Local='' ; } fclose ( $this->fp ); return $nRet ; } function IpToInt($Ip) { $array=explode('.',$Ip); $Int=($array[0] * 256*256*256) + ($array[1]*256*256) + ($array[2]*256) + $array[3]; return $Int; } } function GetIP(){//获取IP return $_SERVER[REMOTE_ADDR]?$_SERVER[REMOTE_ADDR]:$GLOBALS[HTTP_SERVER_VARS][REMOTE_ADDR]; } ?>
jQuery 如何获取浏览器所在的IP地址
$(function () { $("#btnGetIP").click(function () { var jqxhr = $.getJSON("http://jsonip.appspot.com?callback=?", function (data) { alert(data.ip); }) .error(function () { alert("error"); }) }); });
获取本机的真实IP地址
public class Main { public static void main(String[] args) throws SocketException { System.out.println(Main.getRealIp()); } public static String getRealIp() throws SocketException { String localip = null;// 本地IP,如果没有配置外网IP则返回它 String netip = null;// 外网IP Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; boolean finded = false;// 是否找到外网IP while (netInterfaces.hasMoreElements() && !finded) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> address = ni.getInetAddresses(); while (address.hasMoreElements()) { ip = address.nextElement(); if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 外网IP netip = ip.getHostAddress(); finded = true; break; } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 内网IP localip = ip.getHostAddress(); } } } if (netip != null && !"".equals(netip)) { return netip; } else { return localip; } } }
发表评论
-
Spring MVC集成velocity扩展
2013-07-23 17:18 32061、扩展velocity的视图 [code=" ... -
【分布式系统工程实现】CAP理论及系统一致性
2013-03-08 16:05 999印象中CAP理论开始流行 ... -
【分布式系统工程实现】分布式事务
2013-03-08 16:03 1337CAP理论虽然告诉我们,一致性和可用性二者不可兼得,但这并不 ... -
用消息队列和消息应用状态表来消除分布式事务
2013-03-08 16:01 1230由于数据量的巨大,大 ... -
Java编程中“为了性能”尽量要做到的一些地方
2012-09-14 10:33 11861. 尽量在合适的场合使 ... -
Java 5.0多线程编程
2012-08-17 19:16 1027概述 1:三个新加的多线程包 2:C ... -
Thread.setDaemon设置说明
2012-06-28 18:00 1110Thread.setDaemon的用法,经过学习以后了解: ... -
使用MBean获取tomcat和jboss端口
2012-04-10 21:29 2639/** * 根据协议和scheme获取服务端口号 ... -
JVM运行时数据区
2012-03-07 13:15 971JVM定义了若干个程序执行期间使用的数据区域。这个区域里的一些 ... -
使用ibatis防止sql注入
2011-12-19 19:59 4619为了防止SQL注入,iBatis模糊查询时也要避免使用$$来进 ... -
java的引用探讨
2011-12-18 16:03 1095Reference 是一个抽象类,而 SoftReferenc ... -
淘宝网架构概述
2011-12-14 19:45 72众所周知,淘宝网是一 ... -
淘宝网的HttpClient工具
2011-11-23 21:02 47package com.taobao.pegasus.comm ... -
在Tomcat中通过JOTM支持JTA
2011-11-04 15:57 1818<?xml version='1.0' encoding ... -
ClassLoader
2011-11-04 15:11 1163package org.liufei.neta.lib; ... -
仿照Hibernate实现一个SQLite的ORM框架
2011-09-30 20:06 3137那么先来看看使用 实体对象 package test; ... -
sql server2005备份还原
2011-09-22 11:29 9261、先建立一个同名数据库,停止SQL SERVER2005 ... -
java压缩文件
2011-09-20 11:09 1254package org.liufei.net.util; ... -
apache mina开发文件传输服务器
2011-09-19 18:03 7701服务器接收端 /** * */ package o ... -
使用 Apache MINA 2 开发网络应用
2011-09-19 10:01 1121简介: Apache MINA 2 是一个开发高性能和高可伸缩 ...
相关推荐
LabVIEW自动检测获取本机IP地址。 项目可直接运行~
介绍DHCP客户端无法从DHCP服务器获取IP地址的故障排查思路及解决方案。
不通过request 方式和套字节方式获取IP地址.
java 获取IP地址 ...............................................................................
代理ip池(自动采集ip地址 从网站获取ip地址,检测是否可用,并实时更新,存入数据库中)
总共才几行代码,很简易的一个用来获取ip地址的软件
Qt获取IP地址、MAC地址等网卡信息,区分本地网卡、无线网卡和虚拟网卡
自动获取ip地址的BAT批处理 当然我也上传了自动设置IP的批处理代码 大家可以下载后自己改成想要的IP,公司或者企业非常有用。
获得本机IP地址bat文件
本实验利用WinPcap实现ARP协议,从而获取以太网上任意一台主机的IP地址与MAC地址的对应关系。
用VC++编写的获取IP地址的源程序,相信对学习VC++网络编程的人会有用
获取ip地址,并发送到指定邮箱,就这么个软件。
联想工程师专用小工具 自动获取ip地址工具V1.97.1联想工程师专用小工具 自动获取ip地址工具V1.97.1联想工程师专用小工具 自动获取ip地址工具V1.97.1联想工程师专用小工具 自动获取ip地址工具V1.97.1联想工程师专用小...
通过该工具类可以获得请求的IP地址,适用于后台管理系统的信息统计。
java获取IP地址的工具类,使用时传入HttpServletRequest request类型的参数即可。使用例子 public static void logPageError(HttpServletRequest request){IpUtils.getIpAddr(request))}
获取Ip地址代码 获取Ip地址代码
PB11.5获取IP地址及主机名,已经验证
使用intellij idea java开发软件,通过servlet获取访问用户的真实ip地址
完美获取IP地址的PHP代码,接口已经写好,可以直接放到 PHP 程序内做接口使用。