⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.java

📁 一个 非法字符处理 类 很管用 队以一些sql攻击也一起到 反规范作用
💻 JAVA
字号:
package com;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;


public class util {

    //转换为中文字符
    public static String toCN(String s) {
        try {
            String p = new String(s.getBytes("ISO-8859-1"), "GBK");
            return p;
        } catch (Exception ex) {
            System.err.println("util.toCN:" + ex.getMessage());
        }
        return null;
    }

    public static String toISO(String s) {
        try {
            String p = new String(s.getBytes("GBK"), "ISO-8859-1");
            return p;
        } catch (Exception ex) {
            System.err.println("util.toISO:" + ex.getMessage());
        }
        return null;
    }

    public static String replace(String con, String tag, String rep) {
        int j = 0;
        int i = 0;
        String ret = "";
        int tagLen = tag.length();
        while (i < con.length()) {
            if (con.substring(i).startsWith(tag)) {
                ret = ret + con.substring(j, i) + rep;
                i += tagLen;
                j = i;
            } else {
                i += 1;
            }

        }
        ret += con.substring(j);
        return ret;
    }

    public static String htmlEncode(String text) {

        String ret = replace(text, "<", "&lt;");
        ret = replace(ret, ">", "&gt;");
        ret = replace(ret, "\"", "&quot;");
        ret = replace(ret, " ", "&nbsp;");
        return ret;
    }

    public static String htmlDecode(String text) {

        String ret = replace(text, "&lt;", "<");
        ret = replace(ret, "&gt;", ">");
        ret = replace(ret, "&quot;", "\"");
        ret = replace(ret, "&nbsp;", " ");
        return ret;
    }

    public static String sqlEncode(String sqlStr) {
        return replace(sqlStr, "'", "$$$$");
    }

    public static String sqlDecode(String sqlStr) {
        return replace(sqlStr, "$$$$", "'");
    }

    public static String fixQuote(String str) {
        return replace(str, "'", "''");
    }

    public static String fixDoubleQuote(String str) {
        return replace(str, "\"", "\"\"");
    }

    public static String iso2sql(String str) {
        return replace(fixQuote(toCN(str)),"\r\n","<br>");
    }

    public static String cn2sql(String str) {
        return replace(fixQuote(str),"\r\n","<br>");
    }

    public static int getDepLevel(String deptID) {
        int count = 1;
        for (int i = 0; i < deptID.length(); i++) {
            if (deptID.charAt(i) == '.')
                count++;
        }
        return count;
    }

    public static String myTrim(Object str) {
        if (str == null) {
            return new String("");
        } else {
            return ((String) str).trim();
        }
    }

    private static String getString(HttpServletRequest request, String s) {
        String temp = null;
        try {
            temp = request.getParameter(s).trim();
        } catch (Exception e) {
        }
        return temp;
    }


    public static String getString(HttpServletRequest request, String s, String defaultString) throws Exception {
        String s1 = getString(request, s);
        if (s1 == null) return defaultString;
        return s1;
    }

    public static int getInt(HttpServletRequest request, String s, int defaultInt) {
        try {
            String temp = getString(request, s);
            if (temp == null)
                return defaultInt;
            else
                return Integer.parseInt(temp);
        } catch (NumberFormatException e) {
            return 0;
        }
    }
    public static int parseInt( String s, int defaultInt) {
        try {
            String temp = s;
            if (temp == null)
                return defaultInt;
            else
                return Integer.parseInt(temp);
        } catch (NumberFormatException e) {
            return 0;
        }
    }
    public static long getLong(HttpServletRequest request, String s, long defaultLong) {
        try {
            String temp = getString(request, s);
            if (temp == null)
                return defaultLong;
            else
                return Long.parseLong(temp);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    //转换中文时间窜
    private static String numberToCN(int index1)
    {
      String strCN = "";
      String numberstring="一二三四五六七八九十";
      if(index1 ==0) {strCN="";}
      if(index1 < 10){
        strCN = numberstring.substring(0+(index1-1),index1);
      }
      else if(index1 < 20 ){
        strCN = "十"+numberstring.substring(0+(index1-11),(index1-10));
      }
      else if(index1 < 30 ){
        strCN = "二十"+numberstring.substring(0+(index1-21),(index1-20));
      }
      else{
        strCN = "三十"+numberstring.substring(0+(index1-31),(index1-30));
      }

      return strCN;
  }

    private static String yearToCN(int index1)
    {
      String strCN = "";
      String numberstring="○一二三四五六七八九";

      String year = String.valueOf(index1);
      for (int i=0;i<year.length();i++)
      {
          int tmp = Integer.parseInt(year.substring(i,i+1));
          strCN = strCN + numberstring.substring(tmp,tmp+1);
      }
      return strCN;
  }

    public static String date2CN(String datestr)
    {
        String strCN= "";
        try
        {
           String[] e1 = datestr.split("-");
           strCN = yearToCN(Integer.parseInt(e1[0])) + "年" + numberToCN(Integer.parseInt(e1[1])) + "月" + numberToCN(Integer.parseInt(e1[2])) + "日";
        }catch (Exception e)
        {

        }
        return strCN;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -