313c6eec5564bf4bf3d1d10bb0c506d2797e7523.svn-base 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.chinacreator.videoalliance.order.dao;
  2. import java.sql.SQLException;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import javax.servlet.http.HttpServletRequest;
  6. import org.apache.commons.lang.StringUtils;
  7. import org.springframework.stereotype.Component;
  8. import com.chinacreator.common.util.RequestUtil;
  9. import com.frameworkset.common.poolman.SQLExecutor;
  10. /**
  11. * 根据IP匹配渠道标识,用于直接调order.do的请求,渠道标识加统一前缀
  12. * @author xu.zhou
  13. * @date
  14. */
  15. @Component
  16. public class OrderchannelIpmatchDao {
  17. /**
  18. * 用IP的前三个段位到数据库进行模糊匹配,把能匹配上的IP再进行第四个段位的匹配
  19. * @param channel
  20. * @param cpid
  21. * @param spid
  22. * @param request
  23. * @return
  24. */
  25. public String getChannel(String channel, String cpid, String spid, HttpServletRequest request){
  26. if(!"bilibl".equals(cpid) || !"979".equals(spid)) {
  27. return channel;
  28. }
  29. if(StringUtils.isEmpty(channel)) channel = "";
  30. if(channel.startsWith("WP_")){//以WP_开头的,是自己的订购页面过来的
  31. return channel;
  32. }
  33. String orderchannel = "";
  34. try {
  35. String ip = RequestUtil.getIpAddr(request);
  36. if(StringUtils.isEmpty(ip)) {
  37. return channel;
  38. }
  39. //去掉最后一个IP段
  40. String qryip = ip.replace(ip.split("\\.")[3],"");
  41. String lastip = ip.split("\\.")[3];
  42. List<HashMap> dataList = this.getChannelByIp(qryip);
  43. if(dataList != null && dataList.size()>0){
  44. String tmpip = "";
  45. for(HashMap tmpMap : dataList){
  46. tmpip = tmpMap.get("IP").toString();
  47. if(ip.equals(tmpip)){
  48. orderchannel = (String)tmpMap.get("CHANNEL");
  49. break;
  50. }else if(tmpip.contains("~")){
  51. String startip = tmpip.split("\\.")[3].split("~")[0];
  52. String endip = tmpip.split("\\.")[3].split("~")[1];
  53. if(Integer.parseInt(lastip) >= Integer.parseInt(startip)
  54. && Integer.parseInt(lastip) <= Integer.parseInt(endip)){
  55. orderchannel = (String)tmpMap.get("CHANNEL");
  56. break;
  57. }
  58. }
  59. }
  60. }
  61. //有渠道匹配,
  62. if(!"".equals(orderchannel)){
  63. orderchannel = orderchannel+"_"+channel;
  64. if(orderchannel.getBytes().length > 20){
  65. //只包含数字英文和下划线
  66. if(orderchannel.matches("[0-9A-Za-z_]*")){
  67. orderchannel = orderchannel.substring(0,20);
  68. }else{//可能包含中文 ,一个中文占两个字节
  69. orderchannel = orderchannel.substring(0,10);
  70. }
  71. }
  72. }else{//无渠道匹配,默认加UK_
  73. orderchannel = "UK_"+channel;
  74. }
  75. } catch (Exception e) {
  76. orderchannel = channel;
  77. e.printStackTrace();
  78. System.out.println("IP匹配渠道出现异常,"+e.getMessage()+",channel=>"+channel);
  79. }
  80. return orderchannel;
  81. }
  82. public List<HashMap> getChannelByIp(String ip) throws SQLException{
  83. String sql = "SELECT CHANNEL,IP FROM TB_CHANNEL_IPMATCH_CONF WHERE STATUS = '0' AND IP LIKE ? ";
  84. return SQLExecutor.queryListWithDBName(HashMap.class, "net3g", sql, ip+"%");
  85. }
  86. public static void main(String[] args) throws Exception {
  87. OrderchannelIpmatchDao dao = new OrderchannelIpmatchDao();
  88. System.out.println("AAAAABBBBBAAAA你ABBBB".matches("[0-9A-Za-z_]*"));
  89. String ip = "43.250.146.10";
  90. //System.out.println(dao.getChannel(channel, cpid, spid, request));
  91. //System.out.println(dao.getChannelByIp(ip));
  92. //System.out.println(ip.split("\\.")[3].split("~")[0]);
  93. //System.out.println("你好".getBytes().length);
  94. //System.out.println("你好".length());
  95. }
  96. }