示例#1
0
  /**
   * 如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,
   * 那么真正的用户端的真实IP则是取X-Forwarded-For中第一个非unknown的有效IP字符串。
   *
   * @param request
   * @return
   */
  public static String getIp(HttpServletRequest request) {
    try {
      request.setCharacterEncoding("UTF-8");
    } catch (UnsupportedEncodingException e) {
    }

    String ip = request.getHeader("X-Forwarder-For");
    if ((Strings.isBlank(ip)) || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("Proxy-Client-Ip");
      if (Strings.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-Ip");
      }
    } else {
      String[] ipArr = ip.split(",");
      for (String IP : ipArr) {
        if (!"unknown".equalsIgnoreCase(IP)) {
          ip = IP;
          break;
        }
      }
    }
    if ((Strings.isBlank(ip)) || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getRemoteAddr();
    }
    if ("0:0:0:0:0:0:0:1".equals(ip) || "localhost".equals(ip) || "0.0.0.1".equals(ip))
      ip = "127.0.0.1";
    return ip;
  }
示例#2
0
  /**
   * @param filename
   * @return
   */
  public static String major(String filename) {

    String[] split = split(filename);
    String major = Strings.isBlank(split[1]) ? split[0] : split[1];
    int dotIndex = major.lastIndexOf(".");
    if (dotIndex == -1) {
      return major;
    }

    return major.substring(0, dotIndex);
  }
示例#3
0
  /**
   * @param filename
   * @return
   */
  public static String suffix(String filename) {
    if (Strings.isBlank(filename)) {
      return "";
    }

    int dotIndex = filename.lastIndexOf(".");
    if (dotIndex == -1) {
      return "";
    }

    return filename.substring(dotIndex + 1);
  }
示例#4
0
  /**
   * @param filename
   * @return
   */
  public static String[] split(String filename) {
    if (Strings.isBlank(filename)) {
      return new String[] {"", ""};
    }

    filename = asUnix(filename);
    int index = filename.lastIndexOf(UNIX_SEPERATOR);
    if (index == -1) {
      return new String[] {filename, ""};
    }

    return new String[] {filename.substring(0, index), filename.substring(index)};
  }
示例#5
0
文件: Times.java 项目: hhFate/FateCms
  /**
   * 根据字符串得到相对于 "UTC 1970-01-01 00:00:00" 的绝对毫秒数。 本函数假想给定的时间字符串是本地时间。所以计算出来结果后,还需要减去时差
   *
   * <p>支持的时间格式字符串为:
   *
   * <pre>
   * yyyy-MM-dd HH:mm:ss
   * yyyy-MM-dd HH:mm:ss.SSS
   * yy-MM-dd HH:mm:ss;
   * yy-MM-dd HH:mm:ss.SSS;
   * yyyy-MM-dd;
   * yy-MM-dd;
   * HH:mm:ss;
   * HH:mm:ss.SSS;
   * </pre>
   *
   * 时间字符串后面可以跟 +8 或者 +8:00 表示 GMT+8:00 时区。 同理 -9 或者 -9:00 表示 GMT-9:00 时区
   *
   * @param ds 时间字符串
   * @param tz 你给定的时间字符串是属于哪个时区的
   * @return 时间
   * @see #_P_TIME
   */
  public static long ams(String ds, TimeZone tz) {
    Matcher m = _P_TIME.matcher(ds);
    if (m.find()) {
      int yy = _int(m, 2, 1970);
      int MM = _int(m, 4, 1);
      int dd = _int(m, 6, 1);

      int HH = _int(m, 9, 0);
      int mm = _int(m, 11, 0);
      int ss = _int(m, 13, 0);

      int ms = _int(m, 16, 0);

      /*
       * zozoh: 先干掉,还是用 SimpleDateFormat 吧,"1980-05-01 15:17:23" 之前的日子
       * 得出的时间竟然总是多 30 分钟 long day = (long) D1970(yy, MM, dd); long MS =
       * day * 86400000L; MS += (((long) HH) * 3600L + ((long) mm) * 60L +
       * ss) * 1000L; MS += (long) ms;
       *
       * // 如果没有指定时区 ... if (null == tz) { // 那么用字符串中带有的时区信息, if
       * (!Strings.isBlank(m.group(17))) { tz =
       * TimeZone.getTimeZone(String.format("GMT%s%s:00", m.group(18),
       * m.group(19))); // tzOffset = Long.parseLong(m.group(19)) // *
       * 3600000L // * (m.group(18).charAt(0) == '-' ? -1 : 1);
       *
       * } // 如果依然木有,则用系统默认时区 else { tz = TimeZone.getDefault(); } }
       *
       * // 计算 return MS - tz.getRawOffset() - tz.getDSTSavings();
       */
      String str = String.format("%04d-%02d-%02d %02d:%02d:%02d.%03d", yy, MM, dd, HH, mm, ss, ms);
      SimpleDateFormat df = (SimpleDateFormat) DF_DATE_TIME_MS4.clone();
      // 那么用字符串中带有的时区信息 ...
      if (null == tz && !Strings.isBlank(m.group(17))) {
        tz = TimeZone.getTimeZone(String.format("GMT%s%s:00", m.group(18), m.group(19)));
      }
      // 指定时区 ...
      if (null != tz) df.setTimeZone(tz);
      // 解析返回
      try {
        return df.parse(str).getTime();
      } catch (ParseException e) {
        throw new RuntimeException(e);
      }
    }
    throw new RuntimeException("Unexpect date format " + ds);
  }
示例#6
0
文件: Times.java 项目: hhFate/FateCms
 /**
  * 方便的把时间换算成毫秒数
  *
  * <p>支持几个单位, s(秒), m(分钟), h(小时), d(天)
  *
  * <p>比如:
  *
  * <p>100s -> 100000 <br>
  * 2m -> 120000 <br>
  * 3h -> 10800000 <br>
  *
  * @param tstr
  * @return
  */
 public static long toMillis(String tstr) {
   if (Strings.isBlank(tstr)) {
     return 0;
   }
   tstr = tstr.toLowerCase();
   // FIXME 稍后改成正则判断
   String tl = tstr.substring(0, tstr.length() - 1);
   String tu = tstr.substring(tstr.length() - 1);
   if (TIME_S_EN.equals(tu)) {
     return T_1S * Long.valueOf(tl);
   }
   if (TIME_M_EN.equals(tu)) {
     return T_1M * Long.valueOf(tl);
   }
   if (TIME_H_EN.equals(tu)) {
     return T_1H * Long.valueOf(tl);
   }
   if (TIME_D_EN.equals(tu)) {
     return T_1D * Long.valueOf(tl);
   }
   return Long.valueOf(tstr);
 }
示例#7
0
文件: Times.java 项目: hhFate/FateCms
 private static int _int(Matcher m, int index, int dft) {
   String s = m.group(index);
   if (Strings.isBlank(s)) return dft;
   return Integer.parseInt(s);
 }
示例#8
0
 /**
  * @param path
  * @return
  */
 public static String asPackage(String path) {
   if (Strings.isBlank(path)) {
     return path;
   }
   return path.replaceAll("[/|\\\\]+", "\\.");
 }
示例#9
0
 /**
  * @param path
  * @return
  */
 public static String asWindow(String path) {
   if (Strings.isBlank(path)) {
     return path;
   }
   return path.replaceAll("[/|\\\\]+", Matcher.quoteReplacement(WINDOW_SEPERATOR));
 }
示例#10
0
 /**
  * @param path
  * @return
  */
 public static String asUnix(String path) {
   if (Strings.isBlank(path)) {
     return path;
   }
   return path.replaceAll("[/|\\\\]+", UNIX_SEPERATOR);
 }
示例#11
0
 /**
  * @param path
  * @return
  */
 public static String asPlatform(String path) {
   if (Strings.isBlank(path)) {
     return path;
   }
   return path.replaceAll("[/|\\\\]+", Matcher.quoteReplacement(File.separator));
 }
示例#12
0
 public static String notBlank(String text, String template, Object... args) {
   if (Strings.isBlank(text)) {
     throw new IllegalArgumentException(Strings.format(template, args));
   }
   return text;
 }
示例#13
0
 /**
  * Assert that the given String has valid text content; that is, it must not be <code>null</code>
  * and must contain at least one non-whitespace character.
  *
  * <pre class="code">
  * Assert.notBlank(name, &quot;'name' must not be empty&quot;);
  * </pre>
  *
  * @param text the String to check
  * @param message the exception message to use if the assertion fails
  * @see StringUtils#notBlank
  */
 public static String notBlank(String text, String message) {
   if (Strings.isBlank(text)) {
     throw new IllegalArgumentException(message);
   }
   return text;
 }