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

📄 commonutil.java

📁 招标投标网上系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        //当输入字符为空,或空对象时,返回空串
        if ( strPara == null || strPara.length() == 0 )
        {
            return "";
        }
        //当输入的字符中没有要转换的字符时,直接返回
        if ( strPara.indexOf( "\"" ) == -1 )
        {
            return strPara;
        }
        //有要进行转换的字符
        StringBuffer sBuf = new StringBuffer( strPara.length() );
        char c;
        for ( int i = 0; i < strPara.length(); i++ )
        {
            c = strPara.charAt( i );
            if ( c == repBy )
            {
                sBuf.append( rep );
            } else
            {
                sBuf.append( c );
            }
        }
        return sBuf.toString();
    }


    /**
     *  分析输入字符串中的字符把双引号转换成编码形式( & #34;)。(perfer) <p>
     *
     *  在jsp页面中在查询时在hidden变量中保存数值时,为了避免与html中的双引号发生冲突,而被截断</p> <p>
     *
     *  可以使用此函数。</p> <p>
     *
     *  例如:&#60;input type=text name="searchtext" value="&#60;%=Adaption.<b>
     *  parseQuote</b> (strName)%&#62;"&#62;<p>
     *
     *
     *
     *@param  strPara  要转换的字符串
     *@return          被转换后的字符串
     */
    public final static String parseQuote( String strPara )
    {
        return parseStr( strPara, '"', "&#34;" );
    }


    /**
     *  分析输入字符串中的字符把双引号转换成编码形式( & #34;)。(perfer) <p>
     *
     *  在jsp页面中在查询时在hidden变量中保存数值时,为了避免与html中的双引号发生冲突,而被截断</p> <p>
     *
     *  可以使用此函数。</p> <p>
     *
     *  例如:&#60;input type=text name="searchtext" value="&#60;%=Adaption.<b>
     *  parseQuote</b> (strName)%&#62;"&#62;<p>
     *
     *
     *
     *@param  strPara  要转换的字符串
     *@return          被转换后的字符串
     */
    public final static String parseQuoteByJS( String strPara )
    {
        return parseStr( strPara, '"', "\\\"" );
    }
    public final static String parseEnterByP( String strPara )
    {
        return parseStr( strPara, '\n', "<p>" );
    }



    /**
     *  分析输入字符串中的字符把"\n"转换成html中的换行&#60;br&#62;形式。 <p>
     *
     *  在jsp页面中在查询时在页面显示数据时,为了正确显示换行</p> <p>
     *
     *  可以使用此函数。</p>
     *
     *@param  strPara  要转换的字符串
     *@return          被转换后的字符串
     */
    public final static String parseEnter( String strPara )
    {
        //当输入字符为空,或空对象时,返回空串
        if ( strPara == null || strPara.length() == 0 )
        {
            return "";
        }
        //当输入的字符中没有要转换的字符时,直接返回
        if ( strPara.indexOf( "\n" ) == -1 )
        {
            return strPara;
        }
        //有要进行转换的字符
        StringBuffer sBuf = new StringBuffer( strPara.length() );
        char c;
        for ( int i = 0; i < strPara.length(); i++ )
        {
            c = strPara.charAt( i );
            if ( c == '\n' )
            {
                sBuf.append( "<br>" );
            } else
            {
                sBuf.append( c );
            }
        }
        return sBuf.toString();
    }


    /**
     *  分析输入字符串中的字符把"\n"转换成html中的换行&#60;br&#62;形式,同时过滤字符串中包含的Html,并转换双引号(perfer)。
     *  <p>
     *
     *  在jsp中在页面显示数据时,为了正确显示换行</p> <p>
     *
     *  可以使用此函数。</p>
     *
     *@param  strPara  要转换的字符串
     *@return          被转换后的字符串
     */
    public final static String parseHEQuote( String strPara )
    {
        //当输入字符为空,或空对象时,返回空串
        if ( strPara == null || strPara.length() == 0 )
        {
            return "";
        }
        //当输入的字符中没有要转换的字符时,直接返回
        if ( strPara.indexOf( "\n" ) == -1 && strPara.indexOf( "<" ) == -1 && strPara.indexOf( ">" ) == -1 && strPara.indexOf( "\"" ) == -1 )
        {
            return strPara;
        }
        //有要进行转换的字符
        StringBuffer sBuf = new StringBuffer( strPara.length() );
        char c;
        for ( int i = 0; i < strPara.length(); i++ )
        {
            c = strPara.charAt( i );
            if ( c == '\n' )
            {
                sBuf.append( "<br>" );
            } else if ( c == '<' )
            {
                sBuf.append( "&#60;" );
            } else if ( c == '>' )
            {
                sBuf.append( "&#62;" );
            } else if ( c == '\"' )
            {
                sBuf.append( "&#34;" );
            } else
            {
                sBuf.append( c );
            }
        }
        return sBuf.toString();
    }


    /**
     *  分析输入字符串中包含的Html,并转换双引号。(perfer) <p>
     *
     *  在jsp中在页面显示数据时,为了正确显示换行</p> <p>
     *
     *  可以使用此函数。</p>
     *
     *@param  strPara  要转换的字符串
     *@return          被转换后的字符串
     */
    public final static String parseHQuote( String strPara )
    {
        //当输入字符为空,或空对象时,返回空串
        if ( strPara == null || strPara.length() == 0 )
        {
            return "";
        }
        //当输入的字符中没有要转换的字符时,直接返回
        if ( strPara.indexOf( "\n" ) == -1 && strPara.indexOf( "<" ) == -1 && strPara.indexOf( ">" ) == -1 && strPara.indexOf( "\"" ) == -1 )
        {
            return strPara;
        }
        //有要进行转换的字符
        StringBuffer sBuf = new StringBuffer( strPara.length() );
        char c;
        for ( int i = 0; i < strPara.length(); i++ )
        {
            c = strPara.charAt( i );
            if ( c == '<' )
            {
                sBuf.append( "&#60;" );
            } else if ( c == '>' )
            {
                sBuf.append( "&#62;" );
            } else if ( c == '\"' )
            {
                sBuf.append( "&#34;" );
            } else
            {
                sBuf.append( c );
            }
        }
        return sBuf.toString();
    }


    /**
     *  对特殊字符进行转义处理
     *
     *@param  chkstr  参数描述
     *@return         描述返回值信息
     */
    public static String checkStr( String chkstr )
    {
        if ( chkstr == null || chkstr.length() == 0 )
        {
            return chkstr;
        }
        StringBuffer buf = new StringBuffer();
        char temp;
        for ( int i = 0; i < chkstr.length(); i++ )
        {
            temp = chkstr.charAt( i );
            if ( temp == '\"' )
            {
                buf.append( "&quot;" );
            } else if ( temp == '\'' )
            {
                buf.append( "&#39;" );
            } else if ( temp == '\\' )
            {
                buf.append( "&#92;" );
            } else if ( temp == '<' )
            {
                buf.append( "&#60;" );
            } else if ( temp == '>' )
            {
                buf.append( "&#62;" );
            } else if ( temp == '%' )
            {
                buf.append( "&#37;" );
            } else if ( temp == ' ' )
            {
                buf.append( "&nbsp;" );
            } else
            {
                buf.append( temp );
            }
        }
        return buf.toString();
    }


    ////////////////////////////下面函数提供servlet的基本操作

    /**
     *  清除缓存
     *
     *@param  response              参数描述
     *@exception  ServletException  异常描述
     */
    public static void clearCache( HttpServletResponse response )
        throws ServletException
    {
        response.setHeader( "Pragma", "No-cache" );
        response.setHeader( "Cache-Control", "no-cache" );
        response.setDateHeader( "Expires", 0 );
        response.setHeader( "Cache", "F" );
        response.setHeader( "Expires", "0" );
    }


    //////////////////////////下面函数提供调试程序的基本操作

    /**
     *  输出字符串到指定的日志,用来测试用
     *
     *@param  str   要输出的字符串
     *@param  path  要输出的目标文件
     */
    public static void log( String str, String path )
    {   PrintWriter log=null;
        FileWriter fw=null;
        try
        {
            fw = new FileWriter( path, true );
            log = new PrintWriter( fw, true );
            log.println( new java.util.Date() + ":" + str );

        }catch(IOException ex){

        } finally{
            try{
              if(fw!=null){
                fw.close();
                log.close();
              }
            }catch(IOException ex){
            }
         }

    }



    /**
     *  输出布尔到指定的日志,用来测试用
     *
     *@param  str  要输出的字符串
     */
    public static void log( boolean str )
    {
        try
        {
            PrintWriter log = new PrintWriter( new FileWriter( "/log.txt", true ), true );
            log.println( new java.util.Date() + ":" + str );
        } catch ( Exception e )
        {
        }
    }


    /**
     *  输出异常到指定的日志,用来测试用
     *
     *@param  e  参数描述
     */
    public static void log( Exception e )
    {
        try
        {
            PrintWriter log = new PrintWriter( new FileWriter( "/log.txt", true ), true );
            ByteArrayOutputStream ostr = new ByteArrayOutputStream();
            e.printStackTrace( new PrintStream( ostr ) );
            log.println( new java.util.Date() + ":" + ostr.toString() );
        } catch ( Exception ex )
        {
        }
    }


    //////////////////////////////////////////其他一些函数
    /**
     *  产生当前时间戳
     *
     *@return        String 当前时间字符串 例如 '2002-10-14 19:45:00'
     *@deprecated    请参考 public synchronized static String productDate()
     */
    public static synchronized String productDate()
    {
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        Date dt = new Date();
        String id = sdf.format( dt );
        return id;
    }
    public static synchronized String formatDate(java.util.Date date)
    {
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        String id = sdf.format( date );
        return id;
    }

    /**
     *  产生当前时间戳
     *
     *@return        String 当前时间字符串 例如 '2002-10-14 19:45:00'
     *@deprecated    请参考 public synchronized static String productDate()
     */
    public static synchronized String productDate(String strDate,String pattenold,String pattennew) throws Exception
    {
        SimpleDateFormat sdf = new SimpleDateFormat( pattenold );
        Date dt = sdf.parse(strDate);
        sdf.applyPattern(pattennew);
        String id = sdf.format(dt);
        return id;
    }


    /**
     *  产生当前时间戳
     *
     *@return        String 当前时间字符串 例如 '2002-10-14 19:45:00'
     *@deprecated    请参考 public synchronized static String productDate()
     */
    public static synchronized String getCurrentDate()
    {
        return productDate();
    }



    /**
     *  产生指定格式的当前时间戳 <p>
     *
     *  例如 '2002-10-14 19:45:00'</p>

⌨️ 快捷键说明

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