123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- package com.chinacreator.videoalliance.query.dao;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.springframework.stereotype.Component;
- import com.chinacreator.common.dao.ExecutorDao;
- import com.chinacreator.common.util.MD5;
- import com.chinacreator.videoalliance.common.util.DataSource;
- import com.chinacreator.videoalliance.query.bean.IpInfoBean;
- @Component
- public class QueryIPDao extends ExecutorDao{
- private static List<IpInfoBean> IpAll = null;
- private static Map<String,String> map = null;
-
- private static Map<String,List<IpInfoBean>> ipsMap = null;
-
- public static void clearCache(){
- IpAll = null;
- map = null;
- }
-
- private List<IpInfoBean> queryIpInfoAll() throws SQLException{
- if(IpAll==null || IpAll.size()<1){
- IpAll=this.getExecutor().queryListBeanWithDBName(IpInfoBean.class, DataSource.NET3G, "queryIpInfoAll",null);
- }
- return IpAll;
- }
-
- private List<IpInfoBean> queryIpInfoAll(String key) throws SQLException{
- if(key == null || "".equals(key.trim())){
- return null;
- }
-
- if(ipsMap == null){
- ipsMap = new HashMap<String, List<IpInfoBean>>();
- }
-
- List<IpInfoBean> list = ipsMap.get(key);
- if(list == null || list.size()<=0){
- IpInfoBean bo = new IpInfoBean();
- bo.setKey(Long.parseLong(key));
- list = this.getExecutor().queryListBeanWithDBName(IpInfoBean.class, DataSource.NET3G, "queryIpInfoAllByKey",bo);
- ipsMap.put(key+"", list);
- }
-
- return list;
- }
-
- /**
- * 查询IP信息
- *
- * @param bean
- * @return
- * @throws SQLException
- * @author 科创·毛燕龙
- * @datetime 2016年11月4日 上午9:57:46
- */
- public List<IpInfoBean> queryIpInfoBak(IpInfoBean bean) throws SQLException{
- List<IpInfoBean> list = this.queryIpInfoAll();
- if(list != null && list.size()>0){
- List<IpInfoBean> qyeryList = new ArrayList<IpInfoBean>();
- for(IpInfoBean bo :list){
- if(bean.getIp_value()>=bo.getStart_ip_value()
- && bean.getIp_value()<=bo.getEnd_ip_value()){
- qyeryList.add(bo);
- break;
- }
- }
- return qyeryList;
- }else{
- return this.getExecutor().queryListBeanWithDBName(IpInfoBean.class, DataSource.NET3G, "queryIpInfo",bean);
- }
-
- }
-
- /**
- * 查询IP信息
- *
- * @param bean
- * @return
- * @throws SQLException
- * @author 科创·毛燕龙
- * @datetime 2016年11月4日 上午9:57:46
- */
- public List<IpInfoBean> queryIpInfo(IpInfoBean bean) throws SQLException{
- List<IpInfoBean> list = this.queryIpInfoAll(bean.getKey()+"");
- if(list != null && list.size()>0){
- List<IpInfoBean> qyeryList = new ArrayList<IpInfoBean>();
- for(IpInfoBean bo :list){
- if(bean.getIp_value()>=bo.getStart_ip_value()
- && bean.getIp_value()<=bo.getEnd_ip_value()){
- qyeryList.add(bo);
- break;
- }
- }
- return qyeryList;
- }else{
- return this.getExecutor().queryListBeanWithDBName(IpInfoBean.class, DataSource.NET3G, "queryIpInfo",bean);
- }
-
- }
-
- private Map<String,String> queryCpidAllForIp() throws SQLException{
- if(map==null){
- List<IpInfoBean> cpidAll=this.getExecutor().queryListBeanWithDBName(IpInfoBean.class, DataSource.NET3G, "queryCpidAllForIp",null);
- if(cpidAll != null && cpidAll.size()>0){
- map = new HashMap<String, String>();
- for(IpInfoBean bo :cpidAll){
- map.put(bo.getCpid(), bo.getSignkey());
- }
- }
- }
- return map;
- }
-
- /**
- * 查询CPID的signkey信息
- *
- * @param bean
- * @return
- * @throws SQLException
- * @author 科创·毛燕龙
- * @datetime 2016年11月4日 上午9:56:58
- */
- public IpInfoBean queryCpidForIp(IpInfoBean bean) throws SQLException{
- Map<String,String> map = this.queryCpidAllForIp();
- IpInfoBean cp = null;
- if(map != null){
- String key = map.get(bean.getCpid());
- if(key != null && !"".equals(key.trim())){
- cp = new IpInfoBean();
- cp.setCpid(bean.getCpid());
- cp.setSignkey(key);
- }
- }
-
- if(cp==null){
- List<IpInfoBean> list = this.getExecutor().queryListBeanWithDBName(IpInfoBean.class, DataSource.NET3G, "queryCpidForIp",bean);
- if(list != null && list.size()>0){
- cp = list.get(0);
- map = null;
- }
- }
- return cp;
- }
-
- /**
- * 保存没有查询到归属信息IP
- *
- * @param bean
- * @throws SQLException
- * @author 科创·毛燕龙
- * @datetime 2016年11月4日 上午9:56:17
- */
- public void insertInfoNoQuery(IpInfoBean bean) throws SQLException {
- getExecutor().insertBean(DataSource.NET3G, "insertIpInfoNoQuery", bean);
- }
-
- /**
- * 保存没有查询到归属信息IP
- *
- * @param bean
- * @throws SQLException
- * @author 科创·毛燕龙
- * @datetime 2016年11月4日 上午9:56:17
- */
- public void insertInfo(IpInfoBean bean) throws SQLException {
- getExecutor().insertBean(DataSource.NET3G, "insertIpInfoQuery", bean);
- }
-
-
- public static void main(String[] args) throws Exception {
-
- QueryIPDao dao = new QueryIPDao();
- //
- // IpInfoBean bean = new IpInfoBean();
- // bean.setIp("172.18.131.51");
- //
- // System.out.println(dao.queryIpInfo(bean).get(0).getStart_ip());
-
-
- String timestamp = new Date().getTime()/1000+"";
- String cpid = "snPPTV";
- String key = "fce1a4b63dfc4e41bc7gh2d1e6534a4f";
- // String cpid = "tencent";
- // String key = "123";
- String sign = MD5.MD5Encode(cpid+timestamp+key);
- String ip = "222.129.192.225";
-
- String url = "http://127.0.0.1/videoalliance/ipquery.do?timestamp="+timestamp+"&cpid="+cpid+"&sign="+sign+"&ip="+ip;
- System.out.println(url);
-
- // System.out.println((256L*256L*256L*172L+256L*256L*18L+256L*131L+0L));
- // System.out.println((256L*256L*256L*172L+256L*256L*18L+256L*131L+255L));
-
- // File file = new File("D://ipUTF8.txt");
- //
- // //文件读取字符流
- // BufferedReader reader = new BufferedReader(
- // new InputStreamReader(new FileInputStream(file),"utf-8"));
- //
- // //日志记录按行读取,一行为一条记录
- // String line = null;
- // while((line = reader.readLine()) !=null && !"".equals(line)){
- // IpInfoBean bo = new IpInfoBean();
- //
- // String[] test = line.split(" ");
- // int j = 1;
- // String area = null;
- // for(int i=0;i<test.length;i++){
- // if(test[i] != null && !"".equals(test[i].trim())){
- // if(j==1){
- // bo.setStart_ip(test[i]);
- // j=2;
- // }else if(j==2){
- // bo.setEnd_ip(test[i]);
- // j=3;
- // }else if(j==3){
- // j=4;
- // area = test[i];
- // }
- // }
- // }
- //
- // int k = line.indexOf(area);
- // area = line.substring(k,line.length());
- // bo.setArea(area);
- //
- // String op = "";
- // int k2 = area.indexOf("电信");
- // if(k2>=0){
- // op = op+"电信";
- // }
- // k2 = area.indexOf("联通");
- // if(k2>=0){
- // op = op+"联通";
- // }
- // k2 = area.indexOf("移动");
- // if(k2>=0){
- // op = op+"移动";
- // }
- // k2 = area.indexOf("铁通");
- // if(k2>=0){
- // if("".equals(op)){
- // op = op+"联通";
- // }
- // }
- // bo.setId(bo.getStart_ip_value()+"");
- // if(op != null && !"".equals(op.trim())){
- // bo.setOperator(op);
- // bo.setCountry("中国");
- // }
- //
- // try {
- // dao.insertInfo(bo);
- // } catch (Exception e) {
- // e.printStackTrace();
- // System.out.println(bo.getStart_ip());
- // System.out.println(bo.getStart_ip_value());
- // }
- //
- // }
-
- }
- }
|