《项目架构那点儿事》——工具类,你喜欢你就拿去

【前言】众所周知,各式各样的Util类为我们提供了便利,也同时减少了我们对底层硬编码的时间,包括对字符串的操作,文件操作,反射的操作,泛型的操作,以及熟知 的分页类,Json解析类、日期工具类等,这里把我开发的项目中用到过的工具类分享出来,都是经过多个项目的开发积累而锤炼出来的,提供给大家交流和学 习。

【目录】
            1.文件操作类 FileUtil
            2.反射工具类 ReflectionUtil
            3.泛型工具类 GenericsUtils
            4.分页组件 Pagenation,PageUtil,Query
            5.Http与servlet工具类 ServletUtils
            6.日期工具类 DateUtil
            7.Json解析类JsonUtil
            8.编码与解码工具类 EncodeUtils

【代码片段】 由于篇幅有限,我只列举EncodeUtils、Pagenation,其他的大家下载附件即可查看。

/**
* 各种格式的编码加码工具类.
* 
* 集成Commons-Codec,Commons-Lang及JDK提供的编解码方法.
* 
* @author fisher
*/
public class EncodeUtils {
 
        private static final String DEFAULT_URL_ENCODING = "UTF-8";
 
 
        /**
         * Hex编码.
         */
        public static String hexEncode(byte[] input) {
                return Hex.encodeHexString(input);
        }
 
        /**
         * Hex解码.
         */
        public static byte[] hexDecode(String input) {
                try {
                        return Hex.decodeHex(input.toCharArray());
                } catch (DecoderException e) {
                        throw new IllegalStateException("Hex Decoder exception", e);
                }
        }
 
        /**
         * Base64编码.
         */
        public static String base64Encode(byte[] input) {
                return new String(Base64.encodeBase64(input));
        }
 
        /**
         * Base64编码, URL安全(将Base64中的URL非法字符如+,/=转为其他字符, 见RFC3548).
         */
        public static String base64UrlSafeEncode(byte[] input) {
                return Base64.encodeBase64URLSafeString(input);
        }
 
        /**
         * Base64解码.
         */
        public static byte[] base64Decode(String input) {
                return Base64.decodeBase64(input);
        }
 
        /**
         * URL 编码, Encode默认为UTF-8.
         */
        public static String urlEncode(String input) {
                try {
                        return URLEncoder.encode(input, DEFAULT_URL_ENCODING);
                } catch (UnsupportedEncodingException e) {
                        throw new IllegalArgumentException(
                                        "Unsupported Encoding Exception", e);
                }
        }
 
        /**
         * URL 解码, Encode默认为UTF-8.
         */
        public static String urlDecode(String input) {
                try {
                        return URLDecoder.decode(input, DEFAULT_URL_ENCODING);
                } catch (UnsupportedEncodingException e) {
                        throw new IllegalArgumentException(
                                        "Unsupported Encoding Exception", e);
                }
        }
 
        /**
         * Html 转码.
         */
        public static String htmlEscape(String html) {
                return StringEscapeUtils.escapeHtml(html);
        }
 
        /**
         * Html 解码.
         */
        public static String htmlUnescape(String htmlEscaped) {
                return StringEscapeUtils.unescapeHtml(htmlEscaped);
        }
 
        /**
         * Xml 转码.
         */
        public static String xmlEscape(String xml) {
                return StringEscapeUtils.escapeXml(xml);
        }
 
        /**
         * Xml 解码.
         */
        public static String xmlUnescape(String xmlEscaped) {
                return StringEscapeUtils.unescapeXml(xmlEscaped);
        }
 

}

/**
* @author fisher
* @description 分页查询对象
*/
 
public class Pagenation {
    
    /** 是否有上一页 */
    private boolean hasPrePage;
    
    /** 是否有下一页 */
    private boolean hasNextPage;
        
    /** 每页记录数 */
    private int everyPage;
    
    /** 总页数 */
    private int totalPage;
       
    /** 当前页 数*/
    private int currentPage;
    
    /** 查询的开始行数*/
    private int beginIndex;
    
    
    public Pagenation(){
        
    }
    
    /** construct the page by everyPage 
     * @param everyPage
     * */
    
    public Pagenation(int everyPage){
        this.everyPage = everyPage;
    }
    
    /** The whole constructor */
    public Pagenation(boolean hasPrePage, boolean hasNextPage,  
                     int everyPage, int totalPage, 
                     int currentPage, int beginIndex) {
        this.hasPrePage = hasPrePage;
        this.hasNextPage = hasNextPage;
        this.everyPage = everyPage;
        this.totalPage = totalPage;
        this.currentPage = currentPage;
        this.beginIndex = beginIndex;
    }
 
    /**
     * @return 
     * Returns the beginIndex.
     */
    public int getBeginIndex() {
        return beginIndex;
    }
    
    /**
     * @param beginIndex 
     * The beginIndex to set.
     */
    public void setBeginIndex(int beginIndex) {
        this.beginIndex = beginIndex;
    }
    
    /**
     * @return 
     * Returns the currentPage.
     */
    public int getCurrentPage() {
        return currentPage;
    }
    
    /**
     * @param currentPage 
     * The currentPage to set.
     */
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    
    /**
     * @return 
     * Returns the everyPage.
     */
    public int getEveryPage(){
        return everyPage;
    }
    
    /**
     * @param everyPage 
     * The everyPage to set.
     */
    public void setEveryPage(int everyPage) {
        this.everyPage = everyPage;
    }
    
    /**
     * @return 
     * Returns the hasNextPage.
     */
    public boolean getHasNextPage(){
        return hasNextPage;
    }
    
    /**
     * @param hasNextPage 
     * The hasNextPage to set.
     */
    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }
    
    /**
     * @return 
     * Returns the hasPrePage.
     */
    public boolean getHasPrePage() {
        return hasPrePage;
    }
    
    /**
     * @param hasPrePage 
     * The hasPrePage to set.
     */
    public void setHasPrePage(boolean hasPrePage) {
        this.hasPrePage = hasPrePage;
    }
    
    /**
     * @return Returns the totalPage.
     * 
     */
    public int getTotalPage() {
        return totalPage;
    }
    
    /**
     * @param totalPage 
     * The totalPage to set.
     */
    public void setTotalPage(int totalPage){
        this.totalPage = totalPage;
    }
    
}

工具类:http://pan.baidu.com/s/1mgKV19m

相关Jar包:http://pan.baidu.com/s/1i3LH08H

 

posted @ 2015-10-27 10:30  ITeachYou我教你  阅读(195)  评论(0编辑  收藏  举报