`

common

    博客分类:
  • JAVA
阅读更多
/**
 * 
 */
package *;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

@SuppressWarnings("unchecked")
public class Util {

    /**
     * 字符串替换,将 source 中的 oldString 全部换成 newString
     * 
     * @param source 源字符串
     * @param oldString 老的字符串
     * @param newString 新的字符串
     * @return 替换后的字符串
     */
    public static String replace(String source, String oldString, String newString) {
        StringBuffer output = new StringBuffer();
        int lengthOfSource = source.length(); // 源字符串长度
        int lengthOfOld = oldString.length(); // 老字符串长度
        int posStart = 0; // 开始搜索位置
        int pos; // 搜索到老字符串的位置
        while ((pos = source.indexOf(oldString, posStart)) >= 0) {
            output.append(source.substring(posStart, pos));
            output.append(newString);
            posStart = pos + lengthOfOld;
        }
        if (posStart < lengthOfSource) {
            output.append(source.substring(posStart));
        }
        return output.toString();
    }

    /**
     * Convenience method to return a String array as a delimited (e.g. CSV) String. E.g. useful for toString()
     * implementations.
     * 
     * @param arr array to display. Elements may be of any type (toString will be called on each element).
     * @param delim delimiter to use (probably a ,)
     */
    public static String arrayToDelimitedString(Object[] arr, String delim) {
        if (arr == null) {
            return "null";
        } else {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < arr.length; i++) {
                if (i > 0) {
                    sb.append(delim);
                }
                sb.append(arr[i]);
            }
            return sb.toString();
        }
    }

    /**
     * 将空字符串转成默认字符串
     * 
     * @param str 要转化的字符串
     * @param dfValue 默认的字符串
     * @return 转化后的字符串
     */
    public static String dealNull(String str, String dfValue) {

        if (str == null) {
            return dfValue;
        } else {
            return str;
        }

    }

    /**
     * 将空字符串转成空字符串
     * 
     * @param str 要转化的字符串
     * @return 转化后的字符串
     */
    public static String dealNull(String str) {

        if (str == null) {
            return "";
        } else {
            return str;
        }

    }

    public static Object dealNull(Object obj) {

        if (obj == null) {
            return (Object) ("");
        } else {
            return obj;
        }

    }

    /**
     * 取消Html标记
     * 
     * @param str 原始文本
     * @return 取消Html标记后的文本
     */
    public static String htmlEncode(String str) {
        if (str != null) {
            str = replace(str, "&", "&amp;");
            str = replace(str, "&amp;amp;", "&amp;");
            str = replace(str, "&amp;quot;", "&quot;");
            str = replace(str, "\"", "&quot;");
            str = replace(str, "&amp;lt;", "&lt;");
            str = replace(str, "<", "&lt;");
            str = replace(str, "&amp;gt;", "&gt;");
            str = replace(str, ">", "&gt;");
            str = replace(str, "&amp;nbsp;", "&nbsp;");
            // str = replace(str," ","&nbsp;");
        }
        return str;
    }

    /**
     * 功能:格式化当前日期成长整型字符串
     * 
     * @return 返回长整型日期时间字符串
     */
    public static String longStringDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(new Date());
    }

    /**
     * 取文件扩展名
     * 
     * @param fileName 文件名
     * @return 文件扩展名
     */
    public static String getFileExt(String fileName) {
        String value = new String();
        int start = 0;
        int end = 0;
        if (fileName == null)
            return null;
        start = fileName.lastIndexOf('.') + 1;
        end = fileName.length();
        value = fileName.substring(start, end);
        if (fileName.lastIndexOf('.') > 0)
            return value;
        else
            return "";

    }

    /**
     * 远程下载文件
     * 
     * @param destUrl
     * @param fileName
     * @return
     */
    public static boolean remoteDownload(String destUrl, String fileName) {
        boolean succeed = true;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        HttpURLConnection httpUrl = null;
        URL url = null;
        byte[] buf = new byte[8096];
        int size = 0;

        try {
            // 建立链接
            url = new URL(destUrl);
            httpUrl = (HttpURLConnection) url.openConnection();
            // 连接指定的资源
            httpUrl.connect();

            if (httpUrl.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("连接资源文件地址[" + destUrl + "]失败.");
                succeed = false;
            }

            // 获取网络输入流
            bis = new BufferedInputStream(httpUrl.getInputStream());
            // 建立文件
            fos = new FileOutputStream(fileName);

            System.out.println("正在获取链接[" + destUrl + "]的内容...\n将其保存为文件[" + fileName + "]");
            if (httpUrl.getResponseCode() != 200) {
                System.out.println("获取文件链接[" + destUrl + "]失败.");
                succeed = false;
            }

            // 保存文件
            while ((size = bis.read(buf)) != -1) {
                fos.write(buf, 0, size);
            }
            System.out.println("资源[" + url + "]下载完毕!!!");
        }
        catch (IOException e) {

            System.out.println("资源[" + url + "]下载失败!!!");

            succeed = false;
        }
        finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (bis != null) {
                    bis.close();
                }
            }
            catch (IOException ex) {
                System.out.println("关闭资源数据流失败!!!");
            }
            httpUrl.disconnect();
        }

        return succeed;
    }

    /**
     * @param arrVec
     * @param arrayName
     * @param exclude to filter
     * @return
     */
    public static String getScriptArray(Vector arrVec, String arrayName, String exclude[]) {
        return getScriptArray(arrVec, arrayName, exclude, false, false);
    }

    /**
     * @param arrVec
     * @param arrayName
     * @param allFlag ALL it shows
     * @return
     */
    public static String getScriptArray(Vector arrVec, String arrayName, boolean allFlag) {
        return getScriptArray(arrVec, arrayName, null, allFlag, false);
    }

    /**
     * @param arrVec
     * @param arrayName
     * @param exclude to filter
     * @param intFlag ----- it shows
     * @return
     */
    public static String getScriptArray(Vector arrVec, String arrayName, String exclude[], boolean intFlag) {
        return getScriptArray(arrVec, arrayName, exclude, false, intFlag);
    }

    /**
     * JSP script
     * 
     * <pre>
     *  Vector vecUsgeYn = codeMgr.getValues(&quot;UseYN&quot;);  
     *  Util.getScriptArray(vecUserLevel, &quot;arrUserLevel&quot;, true)
     *  Util.getScriptArray(vecUserLevel, &quot;arrUserLevel&quot;, null, true)
     *  Util.getScriptArray(vecUserLevel, &quot;arrUserLevel&quot;, null, false)
     *  Util.getScriptArray(vecUserLevel, &quot;arrUserLevel&quot;, null, true, false)
     * </pre>
     * 
     * @param arrVec data interface
     * @param arrayName name of script
     * @param exclude to filter
     * @param allFlag ALL it shows
     * @param initFlag ---- it shows
     * @return
     */
    public static String getScriptArray(Vector arrVec, String arrayName, String exclude[], boolean allFlag,
        boolean initFlag) {
        StringBuffer sb = new StringBuffer();
        sb.append(arrayName + " = new Array ( \r\n");
        if (allFlag)
            sb.append("\tnew Array('ALL',''),\r\n");
        else if (initFlag)
            sb.append("\tnew Array('-------------',''),\r\n");
        for (int idx = 0; idx < arrVec.size(); idx++) {
            String tmpStr[] = (String[]) arrVec.elementAt(idx);
            if (exclude != null) {
                boolean flag = false;
                for (int jdx = 0; jdx < exclude.length; jdx++) {
                    if (tmpStr[0].equals(exclude[jdx]))
                        flag = true;
                }
                if (!flag)
                    sb.append("\tnew Array(\"" + tmpStr[1] + "\",\"" + tmpStr[0] + "\"),\r\n");
            } else {
                sb.append("\tnew Array(\"" + tmpStr[1] + "\",\"" + tmpStr[0] + "\"),\r\n");
            }
        }
        return sb.substring(0, sb.length() - 3) + ");\n";
    }
}
分享到:
评论

相关推荐

    common-image-3.1.1-API文档-中文版.zip

    赠送jar包:common-image-3.1.1.jar; 赠送原API文档:common-image-3.1.1-javadoc.jar; 赠送源代码:common-image-3.1.1-sources.jar; 赠送Maven依赖信息文件:common-image-3.1.1.pom; 包含翻译后的API文档:...

    wildfly-common-1.5.2.Final-API文档-中文版.zip

    赠送jar包:wildfly-common-1.5.2.Final.jar; 赠送原API文档:wildfly-common-1.5.2.Final-javadoc.jar; 赠送源代码:wildfly-common-1.5.2.Final-sources.jar; 赠送Maven依赖信息文件:wildfly-common-1.5.2....

    flink-table-common-1.12.7-API文档-中文版.zip

    赠送jar包:flink-table-common-1.12.7.jar; 赠送原API文档:flink-table-common-1.12.7-javadoc.jar; 赠送源代码:flink-table-common-1.12.7-sources.jar; 赠送Maven依赖信息文件:flink-table-common-1.12.7....

    weixin-java-common-3.5.0-API文档-中文版.zip

    赠送jar包:weixin-java-common-3.5.0.jar; 赠送原API文档:weixin-java-common-3.5.0-javadoc.jar; 赠送源代码:weixin-java-common-3.5.0-sources.jar; 赠送Maven依赖信息文件:weixin-java-common-3.5.0.pom;...

    springfox-swagger-common-3.0.0-API文档-中文版.zip

    赠送jar包:springfox-swagger-common-3.0.0.jar; 赠送原API文档:springfox-swagger-common-3.0.0-javadoc.jar; 赠送源代码:springfox-swagger-common-3.0.0-sources.jar; 赠送Maven依赖信息文件:springfox-...

    sentinel-transport-common-1.8.0-API文档-中文版.zip

    赠送jar包:sentinel-transport-common-1.8.0.jar; 赠送原API文档:sentinel-transport-common-1.8.0-javadoc.jar; 赠送源代码:sentinel-transport-common-1.8.0-sources.jar; 赠送Maven依赖信息文件:sentinel-...

    netty-common-4.1.65.Final-API文档-中英对照版.zip

    赠送jar包:netty-common-4.1.65.Final.jar; 赠送原API文档:netty-common-4.1.65.Final-javadoc.jar; 赠送源代码:netty-common-4.1.65.Final-sources.jar; 赠送Maven依赖信息文件:netty-common-4.1.65.Final....

    springfox-swagger-common-2.7.0-API文档-中文版.zip

    赠送jar包:springfox-swagger-common-2.7.0.jar; 赠送原API文档:springfox-swagger-common-2.7.0-javadoc.jar; 赠送源代码:springfox-swagger-common-2.7.0-sources.jar; 赠送Maven依赖信息文件:springfox-...

    weixin-java-common-3.5.0-API文档-中英对照版.zip

    赠送jar包:weixin-java-common-3.5.0.jar; 赠送原API文档:weixin-java-common-3.5.0-javadoc.jar; 赠送源代码:weixin-java-common-3.5.0-sources.jar; 赠送Maven依赖信息文件:weixin-java-common-3.5.0.pom;...

    netty-common-4.1.68.Final-API文档-中文版.zip

    赠送jar包:netty-common-4.1.68.Final.jar; 赠送原API文档:netty-common-4.1.68.Final-javadoc.jar; 赠送源代码:netty-common-4.1.68.Final-sources.jar; 赠送Maven依赖信息文件:netty-common-4.1.68.Final....

    com.google.common.jar

    com.google.common.annotations com.google.common.base com.google.common.collect com.google.common.io com.google.common.net com.google.common.primitives com.google.common.util.concurrent 源码...

    SpringCloud分布式微服务项目Common通用依赖模块抽离示例代码

    SpringCloud分布式微服务项目Common通用依赖模块抽离示例代码 SpringCloud分布式微服务项目Common通用依赖模块抽离示例代码 SpringCloud分布式微服务项目Common通用依赖模块抽离示例代码 SpringCloud分布式微服务...

    sentinel-transport-common-1.8.0-API文档-中英对照版.zip

    赠送jar包:sentinel-transport-common-1.8.0.jar; 赠送原API文档:sentinel-transport-common-1.8.0-javadoc.jar; 赠送源代码:sentinel-transport-common-1.8.0-sources.jar; 赠送Maven依赖信息文件:sentinel-...

    netty-transport-native-unix-common-4.1.74.Final-API文档-中文版.zip

    赠送jar包:netty-transport-native-unix-common-4.1.74.Final.jar; 赠送原API文档:netty-transport-native-unix-common-4.1.74.Final-javadoc.jar; 赠送源代码:netty-transport-native-unix-common-4.1.74....

    netty-transport-native-unix-common-4.1.73.Final-API文档-中文版.zip

    赠送jar包:netty-transport-native-unix-common-4.1.73.Final.jar; 赠送原API文档:netty-transport-native-unix-common-4.1.73.Final-javadoc.jar; 赠送源代码:netty-transport-native-unix-common-4.1.73....

    common-lang-3.1.1-API文档-中文版.zip

    赠送jar包:common-lang-3.1.1.jar; 赠送原API文档:common-lang-3.1.1-javadoc.jar; 赠送源代码:common-lang-3.1.1-sources.jar; 赠送Maven依赖信息文件:common-lang-3.1.1.pom; 包含翻译后的API文档:common...

    netty-transport-native-unix-common-4.1.73.Final-API文档-中英对照版.zip

    赠送jar包:netty-transport-native-unix-common-4.1.73.Final.jar; 赠送原API文档:netty-transport-native-unix-common-4.1.73.Final-javadoc.jar; 赠送源代码:netty-transport-native-unix-common-4.1.73....

    Microsoft.ReportViewer.Common(16个版本含11.0)

    文件描述: Microsoft.ReportViewer.Common.dll 文件大小: 3.48M X86/X64: X86 更新时间: 2010-12-29 19:23:44 文件 MD5: B90FD7B1C731A8A3CC74FD60D0225B3E 文件版本: 9.0.21022.8 文件描述: Microsoft....

    实用Common.Lisp编程

    这本《Practical Common Lisp》之所以号称Practical,正是因为这本书大量介绍Common Lisp在现实世界中的各种应用方式,算是第一本「入世传教」的Common Lisp著作。《Practical Common Lisp》是目前最畅销的Common ...

    google common jar包

    import com.google.common.base.Preconditions 编译Setting 或SystemUI会用到。欢迎下载!!

Global site tag (gtag.js) - Google Analytics