71cc1f5589118dcf747284d563f22ad3c1983346.svn-base 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package com.chinacreator.videoalliance.query.dao;
  2. import java.sql.SQLException;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import org.springframework.stereotype.Component;
  9. import com.chinacreator.common.dao.ExecutorDao;
  10. import com.chinacreator.common.util.MD5;
  11. import com.chinacreator.videoalliance.common.util.DataSource;
  12. import com.chinacreator.videoalliance.query.bean.IpInfoBean;
  13. @Component
  14. public class QueryIPDao extends ExecutorDao{
  15. private static List<IpInfoBean> IpAll = null;
  16. private static Map<String,String> map = null;
  17. private static Map<String,List<IpInfoBean>> ipsMap = null;
  18. public static void clearCache(){
  19. IpAll = null;
  20. map = null;
  21. }
  22. private List<IpInfoBean> queryIpInfoAll() throws SQLException{
  23. if(IpAll==null || IpAll.size()<1){
  24. IpAll=this.getExecutor().queryListBeanWithDBName(IpInfoBean.class, DataSource.NET3G, "queryIpInfoAll",null);
  25. }
  26. return IpAll;
  27. }
  28. private List<IpInfoBean> queryIpInfoAll(String key) throws SQLException{
  29. if(key == null || "".equals(key.trim())){
  30. return null;
  31. }
  32. if(ipsMap == null){
  33. ipsMap = new HashMap<String, List<IpInfoBean>>();
  34. }
  35. List<IpInfoBean> list = ipsMap.get(key);
  36. if(list == null || list.size()<=0){
  37. IpInfoBean bo = new IpInfoBean();
  38. bo.setKey(Long.parseLong(key));
  39. list = this.getExecutor().queryListBeanWithDBName(IpInfoBean.class, DataSource.NET3G, "queryIpInfoAllByKey",bo);
  40. ipsMap.put(key+"", list);
  41. }
  42. return list;
  43. }
  44. /**
  45. * 查询IP信息
  46. *
  47. * @param bean
  48. * @return
  49. * @throws SQLException
  50. * @author 科创·毛燕龙
  51. * @datetime 2016年11月4日 上午9:57:46
  52. */
  53. public List<IpInfoBean> queryIpInfoBak(IpInfoBean bean) throws SQLException{
  54. List<IpInfoBean> list = this.queryIpInfoAll();
  55. if(list != null && list.size()>0){
  56. List<IpInfoBean> qyeryList = new ArrayList<IpInfoBean>();
  57. for(IpInfoBean bo :list){
  58. if(bean.getIp_value()>=bo.getStart_ip_value()
  59. && bean.getIp_value()<=bo.getEnd_ip_value()){
  60. qyeryList.add(bo);
  61. break;
  62. }
  63. }
  64. return qyeryList;
  65. }else{
  66. return this.getExecutor().queryListBeanWithDBName(IpInfoBean.class, DataSource.NET3G, "queryIpInfo",bean);
  67. }
  68. }
  69. /**
  70. * 查询IP信息
  71. *
  72. * @param bean
  73. * @return
  74. * @throws SQLException
  75. * @author 科创·毛燕龙
  76. * @datetime 2016年11月4日 上午9:57:46
  77. */
  78. public List<IpInfoBean> queryIpInfo(IpInfoBean bean) throws SQLException{
  79. List<IpInfoBean> list = this.queryIpInfoAll(bean.getKey()+"");
  80. if(list != null && list.size()>0){
  81. List<IpInfoBean> qyeryList = new ArrayList<IpInfoBean>();
  82. for(IpInfoBean bo :list){
  83. if(bean.getIp_value()>=bo.getStart_ip_value()
  84. && bean.getIp_value()<=bo.getEnd_ip_value()){
  85. qyeryList.add(bo);
  86. break;
  87. }
  88. }
  89. return qyeryList;
  90. }else{
  91. return this.getExecutor().queryListBeanWithDBName(IpInfoBean.class, DataSource.NET3G, "queryIpInfo",bean);
  92. }
  93. }
  94. private Map<String,String> queryCpidAllForIp() throws SQLException{
  95. if(map==null){
  96. List<IpInfoBean> cpidAll=this.getExecutor().queryListBeanWithDBName(IpInfoBean.class, DataSource.NET3G, "queryCpidAllForIp",null);
  97. if(cpidAll != null && cpidAll.size()>0){
  98. map = new HashMap<String, String>();
  99. for(IpInfoBean bo :cpidAll){
  100. map.put(bo.getCpid(), bo.getSignkey());
  101. }
  102. }
  103. }
  104. return map;
  105. }
  106. /**
  107. * 查询CPID的signkey信息
  108. *
  109. * @param bean
  110. * @return
  111. * @throws SQLException
  112. * @author 科创·毛燕龙
  113. * @datetime 2016年11月4日 上午9:56:58
  114. */
  115. public IpInfoBean queryCpidForIp(IpInfoBean bean) throws SQLException{
  116. Map<String,String> map = this.queryCpidAllForIp();
  117. IpInfoBean cp = null;
  118. if(map != null){
  119. String key = map.get(bean.getCpid());
  120. if(key != null && !"".equals(key.trim())){
  121. cp = new IpInfoBean();
  122. cp.setCpid(bean.getCpid());
  123. cp.setSignkey(key);
  124. }
  125. }
  126. if(cp==null){
  127. List<IpInfoBean> list = this.getExecutor().queryListBeanWithDBName(IpInfoBean.class, DataSource.NET3G, "queryCpidForIp",bean);
  128. if(list != null && list.size()>0){
  129. cp = list.get(0);
  130. map = null;
  131. }
  132. }
  133. return cp;
  134. }
  135. /**
  136. * 保存没有查询到归属信息IP
  137. *
  138. * @param bean
  139. * @throws SQLException
  140. * @author 科创·毛燕龙
  141. * @datetime 2016年11月4日 上午9:56:17
  142. */
  143. public void insertInfoNoQuery(IpInfoBean bean) throws SQLException {
  144. getExecutor().insertBean(DataSource.NET3G, "insertIpInfoNoQuery", bean);
  145. }
  146. /**
  147. * 保存没有查询到归属信息IP
  148. *
  149. * @param bean
  150. * @throws SQLException
  151. * @author 科创·毛燕龙
  152. * @datetime 2016年11月4日 上午9:56:17
  153. */
  154. public void insertInfo(IpInfoBean bean) throws SQLException {
  155. getExecutor().insertBean(DataSource.NET3G, "insertIpInfoQuery", bean);
  156. }
  157. public static void main(String[] args) throws Exception {
  158. QueryIPDao dao = new QueryIPDao();
  159. //
  160. // IpInfoBean bean = new IpInfoBean();
  161. // bean.setIp("172.18.131.51");
  162. //
  163. // System.out.println(dao.queryIpInfo(bean).get(0).getStart_ip());
  164. String timestamp = new Date().getTime()/1000+"";
  165. String cpid = "snPPTV";
  166. String key = "fce1a4b63dfc4e41bc7gh2d1e6534a4f";
  167. // String cpid = "tencent";
  168. // String key = "123";
  169. String sign = MD5.MD5Encode(cpid+timestamp+key);
  170. String ip = "222.129.192.225";
  171. String url = "http://127.0.0.1/videoalliance/ipquery.do?timestamp="+timestamp+"&cpid="+cpid+"&sign="+sign+"&ip="+ip;
  172. System.out.println(url);
  173. // System.out.println((256L*256L*256L*172L+256L*256L*18L+256L*131L+0L));
  174. // System.out.println((256L*256L*256L*172L+256L*256L*18L+256L*131L+255L));
  175. // File file = new File("D://ipUTF8.txt");
  176. //
  177. // //文件读取字符流
  178. // BufferedReader reader = new BufferedReader(
  179. // new InputStreamReader(new FileInputStream(file),"utf-8"));
  180. //
  181. // //日志记录按行读取,一行为一条记录
  182. // String line = null;
  183. // while((line = reader.readLine()) !=null && !"".equals(line)){
  184. // IpInfoBean bo = new IpInfoBean();
  185. //
  186. // String[] test = line.split(" ");
  187. // int j = 1;
  188. // String area = null;
  189. // for(int i=0;i<test.length;i++){
  190. // if(test[i] != null && !"".equals(test[i].trim())){
  191. // if(j==1){
  192. // bo.setStart_ip(test[i]);
  193. // j=2;
  194. // }else if(j==2){
  195. // bo.setEnd_ip(test[i]);
  196. // j=3;
  197. // }else if(j==3){
  198. // j=4;
  199. // area = test[i];
  200. // }
  201. // }
  202. // }
  203. //
  204. // int k = line.indexOf(area);
  205. // area = line.substring(k,line.length());
  206. // bo.setArea(area);
  207. //
  208. // String op = "";
  209. // int k2 = area.indexOf("电信");
  210. // if(k2>=0){
  211. // op = op+"电信";
  212. // }
  213. // k2 = area.indexOf("联通");
  214. // if(k2>=0){
  215. // op = op+"联通";
  216. // }
  217. // k2 = area.indexOf("移动");
  218. // if(k2>=0){
  219. // op = op+"移动";
  220. // }
  221. // k2 = area.indexOf("铁通");
  222. // if(k2>=0){
  223. // if("".equals(op)){
  224. // op = op+"联通";
  225. // }
  226. // }
  227. // bo.setId(bo.getStart_ip_value()+"");
  228. // if(op != null && !"".equals(op.trim())){
  229. // bo.setOperator(op);
  230. // bo.setCountry("中国");
  231. // }
  232. //
  233. // try {
  234. // dao.insertInfo(bo);
  235. // } catch (Exception e) {
  236. // e.printStackTrace();
  237. // System.out.println(bo.getStart_ip());
  238. // System.out.println(bo.getStart_ip_value());
  239. // }
  240. //
  241. // }
  242. }
  243. }