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

📄 toolutil.java

📁 高效海量访问系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @since 2003/04/09
     */
    public static String Trim_len(String TxtData, int i, boolean is) {
        int len = 0;
        String str = "";
        try {
            len = TxtData.length();
            if (len > i) {
                if (is) {
                    str = TxtData.substring(0, i - 3) + "...";
                } else {
                    str = TxtData.substring(0, i);
                }
            } else {
                return TxtData;
            }
        } catch (Exception e) {
            log.error("调用Trim_len()方法时异常:" + e.toString());
        }
        return str;

    }

    /**
     * 判断一个字符串是否为数值
     *
     * @param		TxtData 类型(String)
     * @return		返回值(boolean) (true:字符串为数值 false:字符串不为为数值)
     * @Exception
     * @author wangjm
     * @since 2003/04/09
     */
    public static boolean IsNumeric(String TxtData) {
        TxtData = TxtData.trim();
        int vl = TxtData.length();
        for (int i = 0; i < vl; i++) {
            char c = TxtData.charAt(i);
            if (!((c >= '0' && c <= '9') || c == '.')) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断一个字符串是否为26个字母中的字符
     *
     * @param			TxtData 类型(String)
     * @return		返回值(boolean) (true:字符串26个字母中的字符 false:字符串不全为26个字母中的字符)
     * @Exception
     * @author wangjm
     * @since 2003/04/09
     */
    public static boolean IsAlpha(String TxtData) {
        TxtData = TxtData.trim();
        int vl = TxtData.length();
        for (int i = 0; i < vl; i++) {
            char c = TxtData.charAt(i);
            if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z')) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断一个字符串是否为0-9 和26个字母中的字符
     *
     * @param			TxtData 类型(String)
     * @return		返回值(boolean) (true:字符串0-9 和26个字母中的字符 false:字符串不全为0-9 和26个字母中的字符)
     * @Exception
     * @author wangjm
     * @since 2003/04/09
     */
    public static boolean IsAlNum(String TxtData) {
        TxtData = TxtData.trim();
        int vl = TxtData.length();
        for (int i = 0; i < vl; i++) {
            char c = TxtData.charAt(i);
            if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z')
                    && !(c >= '0' && c <= '9')) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断一个字符串是否为全长
     *
     * @param			TxtData 类型(String)
     * @param			MaxLen 类型(int)
     * @return		返回值(boolean) (true:字符串为MaxLen false:字符串不等于MaxLen)
     * @Exception
     * @author wangjm
     * @since 2003/04/09
     */
    public static boolean IsFullLen(String TxtData, int MaxLen) {
        TxtData = TxtData.trim();
        int l = TxtData.length();
        if (l > 0 && l != MaxLen) {
            return false;
        }
        return true;
    }

    /**
     * 判断一个字符串是否为日期型
     *
     * @param			TxtData 类型(String)
     * @return		返回值(boolean) (true:字符串日期型false:字符串不全为日期型)
     * @Exception
     * @author wangjm
     * @since 2003/04/09
     */
    public static boolean IsDate(String TxtData) {
        TxtData = TxtData.trim();

        if (TxtData.length() > 0) {

            if (!IsNumeric(TxtData) || !IsFullLen(TxtData, 8)) {
                return false;
            }

            int y = new Integer(TxtData.substring(0, 4)).intValue();
            int m = new Integer(TxtData.substring(4, 6)).intValue();
            int d = new Integer(TxtData.substring(6)).intValue();

            try {
                Calendar date = new GregorianCalendar(y, m - 1, d);
                date.setLenient(false);
                date.get(Calendar.DATE);
            } catch (java.lang.IllegalArgumentException e) {
                log.error("调用IsDate()方法时异常:" + e.toString());
                return false;
            }
        }
        return true;
    }

    /**
     * 将数字转换为指定的格式 格式类型类似于
     * ####.000(.后面的0的个数代表保留几位小数)
     * 0.000E0000(返回指定的指数形式)
     * 返回四舍五入后的指定小数位的数字字符串
     * 如:输入 1234.567890 返回 1234.568;或 1.235E0003
     *
     * @param num    String
     * @param format String
     * @return String
     * @throws NumberFormatException
     * @author wangxi
     */
    public static String FormatNum(String num, String format) throws NumberFormatException {
        String rtnum = "0";
        try {
            DecimalFormat df1 = new DecimalFormat(format);
            rtnum = df1.format(new Long(num));
        } catch (NumberFormatException ex) {
            log.error("调用FormatNum()方法时异常:" + ex.toString());
            ex.printStackTrace();
        }
        return rtnum;
    }

    /**
     * 按指定的位数四舍五入指定数字
     *
     * @param num    String
     * @param dotnum String
     * @return String
     * @throws NumberFormatException
     * @author wangxi
     */
    public static float FormatNum(float num, int dotnum) throws NumberFormatException {
        float rtnum = 0;
        try {
            StringBuffer sformat = new StringBuffer("####.0");
            for (int i = 1; i < dotnum; i++) {
                sformat.append("0");
            }
            DecimalFormat df1 = new DecimalFormat(sformat.toString());
            rtnum = Float.parseFloat(df1.format(num));
        } catch (NumberFormatException ex) {
            log.error("调用FormatNum()方法时异常:" + ex.toString());
            ex.printStackTrace();
        }
        return rtnum;
    }


    /**
     * 判断一个字符串是否为时间类型
     *
     * @param		TxtData 类型(String)
     * @return		返回值(boolean) (true:为时间型false:字符串不全为时间)
     * @Exception
     * @author wangjm
     * @since 2003/04/09
     */
    public static boolean IsTime(String TxtData) {
        TxtData = TxtData.trim();

        if (TxtData.length() > 0) {
            if (!IsNumeric(TxtData) || !IsFullLen(TxtData, 6)) {
                return false;
            }
            int h = new Integer(TxtData.substring(0, 2)).intValue();
            int m = new Integer(TxtData.substring(2, 4)).intValue();
            int s = new Integer(TxtData.substring(4)).intValue();
            if (h < 0 || h > 23) {
                return false;
            }
            if (m < 0 || m > 59) {
                return false;
            }
            if (s < 0 || s > 59) {
                return false;
            }
        }
        return true;
    }

    /**
     * 通过指定的日期得到相应的日期字符串
     * editby zouzh
     *
     * @param sTitle String
     * @return	返回值String型 yyyy-MM-dd hh:mm:ss
     */
    public static String dealTitle(String sTitle, int iMax, int iStep) {
        String sReturn = "";
        try {
            if (sTitle.length() > iMax) {
                if (iMax > iStep) {
                    sReturn = sTitle.substring(0, iMax - iStep);
                }
                for (int i = 0; i < iStep; i++) {
                    sReturn += ".";
                }
                //log.info("dealTitle() sReturn:" + sReturn);
            } else {
                sReturn = sTitle;
            }
        } catch (Exception e) {
            log.error("CB.dealTitle()例外:" + e);
        }
        return sReturn;
    }

    public static boolean IsWChar(String TxtData) {
        int vl = TxtData.length();
        for (int i = 0; i < vl; i++) {
            char c = TxtData.charAt(i);
            if (c >= ' ' && c <= '~') { //(U0020--U007e)
                return false;
            }
        }
        return true;
    }

    public static boolean IsKChar(String TxtData) {
        int vl = TxtData.length();
        for (int i = 0; i < vl; i++) {
            char c = TxtData.charAt(i);
            if (c >= '?' || c <= '?') { //(UFF66-UFF9F)
                return false;
            }
        }
        return true;
    }

    //处理xml
    //得到NodeList对象
    public Element getElementObject(String filepath) {
        Element root = null;
        try {
            File f = new File(this.getClass().getResource("/").getPath());
            f = new File(f.getPath() + filepath);
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.
                    newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

            //Document doc = docBuilder.parse(is);
            Document doc = docBuilder.parse(f);
            root = doc.getDocumentElement();

        } catch (Exception e) {
            System.err.println("不能读取属性文件. " + "请确保db.xml在指定的路径中=" + e.toString());
            e.printStackTrace();
        }

        return root;
    }

    /*
     * 用于读取xml文件信息,获得一个element节点的描述值
     */
    public String getValue(Node node) {
        if (node == null)
            return "";
        if (node.getNodeType() != Node.ELEMENT_NODE)
            return "";
        return node.getChildNodes().item(0).getNodeValue();
    }

    /*
     * 用于读取xml文件信息,获得一个element节点的子节点的描述值
     */
    public String getChildNodeValue(Node node, String childName) {
        if (node == null)
            return "";
        if (node.getNodeType() != Node.ELEMENT_NODE)
            return "";
        return getValue(((Element) node).getElementsByTagName(childName).item(0));
    }

    /*
     * 用于读取xml文件信息,得到按名称指定的子节点列表
     */
    public NodeList getElementsByTagName(Element element, String tagName) {
        NodeList nodeList = element.getElementsByTagName(tagName);
        return nodeList;
    }

    /**
     * 将串进行MD5加密
     *
     * @param s 原串
     * @return
     * @author 王溪
     * @serialData 2004-07-19
     */

    public static String toMD5String(String sTemp) {
        byte[] bTemp = "".getBytes();
        if (sTemp != null && !sTemp.equals("")) {
            bTemp = sTemp.getBytes();
        }
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            sTemp = new String(md5.digest(bTemp), "iso8859-1");
            //sTemp = md5.digest(bTemp).toString();
        } catch (Exception ex) {
            log.debug("调用cn.com.surekam.common.CB的toMD5String");
            ex.printStackTrace();
        }
        return sTemp;
    }

    /**
     * 将汉字转为UTF8编码的串
     *
     * @param s 原汉字串
     * @return 重新编码后的UTF8编码的串
     * @author 王溪
     * @serialData 2004-07-19
     */
    public static String toUtf8String(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 0 && c <= 255) {
                sb.append(c);
            } else {
                byte[] b;
                try {
                    b = Character.toString(c).getBytes("utf-8");
                } catch (Exception ex) {
                    System.out.println(ex);
                    b = new byte[0];
                }
                for (int j = 0; j < b.length; j++) {
                    int k = b[j];
                    if (k < 0) k += 256;
                    sb.append("%" + Integer.toHexString(k).
                            toUpperCase());
                }
            }
        }
        return sb.toString();
    }

    /**
     * 将字符串转换成ArrayList
     *
     * @param sTemp String
     * @return tempList ArrayList
     * @author zouzh
     * @serialData 2004-09-19
     */

    public static ArrayList strToArrayList(String sTemp) {
        ArrayList tempList = new ArrayList();
        String[] sTempArray = null;
        try {
            sTempArray = getArray(sTemp,",");
            for (int i = 0; i < sTempArray.length; i++) {
                tempList.add(sTempArray[i]);
                log.info("sTempArray[i]:"+sTempArray[i]);
            }
        } catch (Exception ex) {
            log.debug("调用cn.com.surekam.common.strToArrayList");
            ex.printStackTrace();
        }
        return tempList;
    }

	public static String filterHtml(String body) {
		if (body==null){
			return "";
		}		
		body = body.replaceAll("<","&lt;").replaceAll(">","&gt;").replaceAll("\"","&quot;");	
		return body;
	}
	public static String filterHtmlTag(String body) {
		if (body==null){
			return "";
		}		
		body=body.replaceAll("FRAME","");
		body=body.replaceAll("frame","");
		body=body.replaceAll("Frame","");
		body=body.replaceAll("APPLET","");
		body=body.replaceAll("applet","");
		body=body.replaceAll("Applet","");
		body=body.replaceAll("<SCRIPT>","&lt;SCRIPT&gt;");
		body=body.replaceAll("<Script>","&lt;SCRIPT&gt;");
		body=body.replaceAll("<script>","&lt;SCRIPT&gt;");
		body=body.replaceAll("<BODY>","&lt;body&gt;");
		body=body.replaceAll("<Body>","&lt;body&gt;");
		body=body.replaceAll("<body>","&lt;body&gt;");
		body=body.replaceAll("<form>","&lt;form&gt;");
		body=body.replaceAll("<FORM>","&lt;form&gt;");
		body=body.replaceAll("<Form>","&lt;form&gt;");
		return body;
	}
	
	public static String encodeUrl(String sourceurl){
		String temp="";
		String[] url=ToolUtil.getArray(sourceurl,"&");		
		if(url.length>0){
			temp=url[0];
		}			
		for(int i=1;i<url.length;i++){
			if(url[i].indexOf("=")!=-1){
				try {
					temp=temp + "&" + url[i].substring(0,url[i].indexOf("=")+1)+ URLEncoder.encode(url[i].substring(url[i].indexOf("=")+1),"GBK");
				} catch (UnsupportedEncodingException e) {					
					e.printStackTrace();
				}
			}
			
		}	
		return temp;
	}
}

⌨️ 快捷键说明

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