常用工具类封装

日期转换工具类 CommUtil.java

[java]  view plain  copy
  1. package com.util;  
  2.   
  3. import java.text.ParseException;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Date;  
  6.   
  7. /** 
  8.  * 日期转换工具类 
  9.  */  
  10. public class CommUtil {  
  11.   
  12.     /** 
  13.      * 将日期格式转换成yyyy-MM-dd的字符串格式 
  14.      * 返回值如:2010-10-06 
  15.      * @param time 要转换的日期 
  16.      * @return 
  17.      */  
  18.     public static  String dateToString(Date time)  {  
  19.           
  20.         SimpleDateFormat formatter = new  SimpleDateFormat ("yyyy-MM-dd"); //定义将日期格式要换成的格式  
  21.         String stringTime  =  formatter.format(time);  
  22.       
  23.         return  stringTime;  
  24.           
  25.       }  
  26.     /** 
  27.      * 将日期格式转换成yyyyMMdd的字符串格式 
  28.      * 返回值如:2010-10-06 
  29.      * @param time 要转换的日期 
  30.      * @return 
  31.      */  
  32.     public static  String dateTimeToString(Date time)  {  
  33.           
  34.         SimpleDateFormat formatter = new  SimpleDateFormat ("yyyyMMdd"); //定义将日期格式要换成的格式  
  35.         String stringTime  =  formatter.format(time);  
  36.       
  37.         return  stringTime;  
  38.           
  39.       }  
  40.       
  41.        
  42.     /** 
  43.      * 将日期格式转换成yyyy-MM-dd的字符串格式 
  44.      * 返回值如:2010-10-06 
  45.      * @param time 要转换的日期 
  46.      * @return 
  47.      */  
  48.     public static  Date dateToDate(Date time)  {  
  49.           
  50.         SimpleDateFormat formatter = new  SimpleDateFormat ("yyyy-MM-dd"); //定义将日期格式要换成的格式  
  51.         String stringTime  =  formatter.format(time);  
  52.      Date date = null;  
  53.     try {  
  54.         date = formatter.parse(stringTime);  
  55.     } catch (ParseException e) {  
  56.         e.printStackTrace();  
  57.     }  
  58.         return  date;  
  59.           
  60.     }  
  61.       
  62.     /** 
  63.      * 得到当前时间,以字符串表示 
  64.      * @return 
  65.      */  
  66.     public static String getDate(){  
  67.         Date date = new Date();  
  68.         return CommUtil.dateToString(date);  
  69.     }  
  70. }  

日期转换类  DateConverter.java

[java]  view plain  copy
  1. package com.util;  
  2.   
  3. import java.text.DateFormat;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Date;  
  6. import java.util.Map;  
  7.   
  8. import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;  
  9.   
  10. /** 
  11.  * 日期转换类 
  12.  * 
  13.  */  
  14. public class DateConverter extends DefaultTypeConverter {  
  15.     private static final DateFormat[] ACCEPT_DATE_FORMATS = {  
  16.             new SimpleDateFormat("dd/MM/yyyy"),  
  17.             new SimpleDateFormat("yyyy-MM-dd"),  
  18.             new SimpleDateFormat("yyyy/MM/dd") }; //支持转换的日期格式   
  19.   
  20.     @Override   
  21.     public Object convertValue(Map context, Object value, Class toType) {   
  22.         if (toType == Date.class) {  //浏览器向服务器提交时,进行String to Date的转换   
  23.             Date date = null;   
  24.             String dateString = null;   
  25.             String[] params = (String[])value;   
  26.             dateString = params[0];//获取日期的字符串   
  27.             for (DateFormat format : ACCEPT_DATE_FORMATS) {   
  28.                 try {   
  29.                     return format.parse(dateString);//遍历日期支持格式,进行转换   
  30.                 } catch(Exception e) {   
  31.                     continue;   
  32.                 }   
  33.             }   
  34.             return null;   
  35.         }   
  36.         else if (toType == String.class) {   //服务器向浏览器输出时,进行Date to String的类型转换   
  37.             Date date = (Date)value;   
  38.             return new SimpleDateFormat("yyyy-MM-dd").format(date);//输出的格式是yyyy-MM-dd   
  39.         }   
  40.           
  41.         return null;   
  42.     }  
  43. }  

功能更强大的格式化工具类 FormatUtils.java

 

[java]  view plain  copy
  1. package com.util;  
  2.   
  3. import java.text.DecimalFormat;  
  4. import java.text.ParseException;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Date;  
  7.   
  8. /** 
  9.  * 功能更强大的格式化工具类 
  10.  */  
  11. public class FormatUtils {  
  12.     private static SimpleDateFormat second = new SimpleDateFormat(  
  13.             "yy-MM-dd hh:mm:ss");  
  14.   
  15.     private static SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");  
  16.     private static SimpleDateFormat detailDay = new SimpleDateFormat("yyyy年MM月dd日");  
  17.     private static SimpleDateFormat fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");  
  18.     private static SimpleDateFormat tempTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  19.     private static SimpleDateFormat excelDate = new SimpleDateFormat("yyyy/MM/dd");  
  20.       
  21.     /** 
  22.      * 格式化excel中的时间 
  23.      * @param date 
  24.      * @return 
  25.      */  
  26.     public static String formatDateForExcelDate(Date date) {  
  27.         return excelDate.format(date);  
  28.     }  
  29.       
  30.     /** 
  31.      * 将日期格式化作为文件名 
  32.      * @param date 
  33.      * @return 
  34.      */  
  35.     public static String formatDateForFileName(Date date) {  
  36.         return fileName.format(date);  
  37.     }  
  38.   
  39.     /** 
  40.      * 格式化日期(精确到秒) 
  41.      *  
  42.      * @param date 
  43.      * @return 
  44.      */  
  45.     public static String formatDateSecond(Date date) {  
  46.         return second.format(date);  
  47.     }  
  48.       
  49.     /** 
  50.      * 格式化日期(精确到秒) 
  51.      *  
  52.      * @param date 
  53.      * @return 
  54.      */  
  55.     public static String tempDateSecond(Date date) {  
  56.         return tempTime.format(date);  
  57.     }  
  58.   
  59.     public static Date tempDateSecond(String str) {  
  60.         try {  
  61.             return tempTime.parse(str);  
  62.         } catch (ParseException e) {  
  63.             e.printStackTrace();  
  64.         }  
  65.         return new Date();  
  66.     }  
  67.     /** 
  68.      * 格式化日期(精确到天) 
  69.      *  
  70.      * @param date 
  71.      * @return 
  72.      */  
  73.     public static String formatDateDay(Date date) {  
  74.         return day.format(date);  
  75.     }  
  76.       
  77.     /** 
  78.      * 格式化日期(精确到天) 
  79.      *  
  80.      * @param date 
  81.      * @return 
  82.      */  
  83.     public static String formatDateDetailDay(Date date) {  
  84.         return detailDay.format(date);  
  85.     }  
  86.   
  87.     /** 
  88.      * 将double类型的数字保留两位小数(四舍五入) 
  89.      *  
  90.      * @param number 
  91.      * @return 
  92.      */  
  93.     public static String formatNumber(double number) {  
  94.         DecimalFormat df = new DecimalFormat();  
  95.         df.applyPattern("#0.00");  
  96.         return df.format(number);  
  97.     }  
  98.   
  99.     /** 
  100.      * 将字符串转换成日期 
  101.      *  
  102.      * @param date 
  103.      * @return 
  104.      * @throws Exception 
  105.      */  
  106.     public static Date formateDate(String date) throws Exception {  
  107.         return day.parse(date);  
  108.     }  
  109.       
  110.     /** 
  111.      * 将字符日期转换成Date 
  112.      * @param date 
  113.      * @return 
  114.      * @throws Exception 
  115.      */  
  116.     public static Date parseStringToDate(String date) throws Exception {  
  117.         return day.parse(date);  
  118.     }  
  119.       
  120.     public static String formatDoubleNumber(double number) {  
  121.         DecimalFormat df = new DecimalFormat("#");  
  122.         return df.format(number);  
  123.     }  

分页工具类 SharePager.java

[java]  view plain  copy
  1. package com.util;  
  2.   
  3.   
  4. /** 
  5.  * 分页工具类 
  6.  * 
  7.  */  
  8. public class SharePager {  
  9.     private int totalRows; //总行数  
  10.     private int pageSize = 20//每页显示的行数    
  11.     private int currentPage; //当前页号  
  12.     private int totalPages; //总页数   
  13.     private int startRow; //当前页在数据库中的起始行    
  14.      
  15.   
  16.     /** 
  17.      * 默认构造函数 
  18.      */  
  19.     public SharePager()   
  20.     {    
  21.           
  22.     }    
  23.         
  24.     /**默认每页10行 
  25.      * @param totalRows  
  26.      */    
  27.     public SharePager(int totalRows)   
  28.     {    
  29.         this.totalRows = totalRows;    
  30.   
  31.         totalPages =(int) Math.ceil((double)totalRows / (double)pageSize);    
  32.         startRow = 0;    
  33.     }    
  34.         
  35.         
  36.     /**可自定义每页显示多少页 
  37.      * @param totalRows  
  38.      * @param pageSize  
  39.      */    
  40.     public SharePager(int totalRows, int pageSize)   
  41.     {    
  42.         this.totalRows = totalRows;    
  43.         this.pageSize = pageSize;   
  44.         if(this.pageSize<1)                 
  45.             this.pageSize=1;  
  46.         else if(pageSize>20)  
  47.             this.pageSize=20;  
  48.   
  49. //        if(this.pageSize>totalRows){  
  50. //          this.pageSize=(int)totalRows;  
  51. //        }  
  52.           
  53.       
  54.   
  55.         totalPages =(int) Math.ceil((double)totalRows / (double)this.pageSize);     
  56.         currentPage = 1;    
  57.         startRow = 0;    
  58.     }    
  59.         
  60.     /** 
  61.      * 跳转到首页 
  62.      */  
  63.     public void first()   
  64.     {    
  65.         this.currentPage = 1;    
  66.         this.startRow = 0;   
  67.     }    
  68.       
  69.     /** 
  70.      * 跳转到上一页 
  71.      */  
  72.     public void previous()   
  73.     {    
  74.         if (currentPage == 1)   
  75.         {    
  76.             return;    
  77.         }    
  78.         currentPage--;    
  79.         startRow = (currentPage-1) * pageSize;    
  80.    }    
  81.       
  82.     /** 
  83.      * 跳转到下一页 
  84.      */  
  85.     public void next()   
  86.     {    
  87.         if (currentPage < totalPages)  
  88.         {    
  89.             currentPage++;    
  90.         }    
  91.         startRow = (currentPage-1) * pageSize;    
  92.     }    
  93.       
  94.     /** 
  95.      *  跳转到尾页 
  96.      */  
  97.     public void last()   
  98.     {    
  99.         this.currentPage = totalPages;    
  100.         if(currentPage<1){  
  101.             currentPage = 1;  
  102.         }  
  103.         this.startRow = (currentPage-1) * this.pageSize;  
  104.         totalPages =(int) Math.ceil((double)totalRows / (double)this.pageSize);     
  105.           
  106.     }    
  107.       
  108.     /** 
  109.      * 跳转到指定页 
  110.      * @param currentPage   指定的页 
  111.      */  
  112.     public void refresh(int currentPage)   
  113.     {    
  114.           
  115.         if(currentPage < 0)  
  116.         {  
  117.             first();  
  118.         }  
  119.         if (currentPage > totalPages)   
  120.         {    
  121.             last();    
  122.         }else{  
  123.             this.currentPage = currentPage;  
  124.             this.startRow = (currentPage-1) * this.pageSize;  
  125.           }  
  126.    
  127.     }    
  128.        
  129.         
  130.     public int getStartRow()  
  131.     {    
  132.         return startRow;    
  133.     }    
  134.       
  135.     public int getTotalPages()  
  136.     {    
  137.         return totalPages;    
  138.     }    
  139.       
  140.     public int getCurrentPage()  
  141.     {    
  142.         return currentPage;    
  143.     }    
  144.       
  145.     public int getPageSize()   
  146.     {    
  147.         return pageSize;    
  148.     }    
  149.       
  150.     public void setTotalRows(int totalRows)   
  151.     {    
  152.         this.totalRows = totalRows;    
  153.     }    
  154.       
  155.     public void setStartRow(int startRow)  
  156.     {    
  157.         this.startRow = startRow;    
  158.     }    
  159.       
  160.     public void setTotalPages(int totalPages)   
  161.     {    
  162.         this.totalPages = totalPages;    
  163.     }    
  164.       
  165.     public void setCurrentPage(int currentPage)   
  166.     {    
  167.         this.currentPage = currentPage;    
  168.     }   
  169.       
  170.     public void setPageSize(int pageSize)   
  171.     {    
  172.         this.pageSize = pageSize;    
  173.     }    
  174.       
  175.     public int getTotalRows()   
  176.     {    
  177.         return totalRows;    
  178.     }    
  179. 读取Config文件工具类 PropertiesConfig.java

    [java]  view plain  copy
    1. package com.util;  
    2.   
    3. import java.io.BufferedInputStream;  
    4. import java.io.FileInputStream;  
    5. import java.io.InputStream;  
    6. import java.util.Properties;  
    7. /** 
    8.  * 读取Config文件工具类 
    9.  * @version 1.0 
    10.  * @since JDK 1.6 
    11.  */  
    12. public class PropertiesConfig {    
    13.         
    14.     /**  
    15.      * 获取整个配置文件中的属性 
    16.      * @param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties  
    17.      */    
    18.     public static Properties readData(String filePath) {    
    19.         filePath = getRealPath(filePath);  
    20.         Properties props = new Properties();    
    21.         try {    
    22.             InputStream in = new BufferedInputStream(new FileInputStream(filePath));    
    23.             props.load(in);    
    24.             in.close();    
    25.             return props;    
    26.         } catch (Exception e) {    
    27.             e.printStackTrace();    
    28.             return null;    
    29.         }    
    30.     }    
    31.       
    32.     private static String getRealPath(String filePath) {  
    33.         //获取绝对路径 并截掉路径的”file:/“前缀    
    34.         return PropertiesConfig.class.getResource("/" + filePath).toString().substring(6);  
    35.     }  

    MD5编码工具类 MD5Code.java

    [java]  view plain  copy
    1. package com.util;  
    2. /** 
    3.  * MD5编码工具类 
    4.  * 
    5.  */  
    6. public class MD5Code {  
    7.     static final int S11 = 7;  
    8.   
    9.     static final int S12 = 12;  
    10.   
    11.     static final int S13 = 17;  
    12.   
    13.     static final int S14 = 22;  
    14.   
    15.     static final int S21 = 5;  
    16.   
    17.     static final int S22 = 9;  
    18.   
    19.     static final int S23 = 14;  
    20.   
    21.     static final int S24 = 20;  
    22.   
    23.     static final int S31 = 4;  
    24.   
    25.     static final int S32 = 11;  
    26.   
    27.     static final int S33 = 16;  
    28.   
    29.     static final int S34 = 23;  
    30.   
    31.     static final int S41 = 6;  
    32.   
    33.     static final int S42 = 10;  
    34.   
    35.     static final int S43 = 15;  
    36.   
    37.     static final int S44 = 21;  
    38.   
    39.     static final byte[] PADDING = { -128000000000000,  
    40.             0000000000000000000000,  
    41.             0000000000000000000000,  
    42.             0000000 };  
    43.   
    44.     private long[] state = new long[4];// state (ABCD)  
    45.   
    46.     private long[] count = new long[2];// number of bits, modulo 2^64 (lsb  
    47.   
    48.     // first)  
    49.   
    50.     private byte[] buffer = new byte[64]; // input buffer  
    51.   
    52.   
    53.     public String digestHexStr;  
    54.       
    55.     private byte[] digest = new byte[16];  
    56.   
    57.     public String getMD5ofStr(String inbuf) {  
    58.         md5Init();  
    59.         md5Update(inbuf.getBytes(), inbuf.length());  
    60.         md5Final();  
    61.         digestHexStr = "";  
    62.         for (int i = 0; i < 16; i++) {  
    63.             digestHexStr += byteHEX(digest[i]);  
    64.         }  
    65.         return digestHexStr;  
    66.     }  
    67.   
    68.     public MD5Code() {  
    69.         md5Init();  
    70.         return;  
    71.     }  
    72.   
    73.     private void md5Init() {  
    74.         count[0] = 0L;  
    75.         count[1] = 0L;  
    76.         // /* Load magic initialization constants.  
    77.         state[0] = 0x67452301L;  
    78.         state[1] = 0xefcdab89L;  
    79.         state[2] = 0x98badcfeL;  
    80.         state[3] = 0x10325476L;  
    81.         return;  
    82.     }  
    83.   
    84.     private long F(long x, long y, long z) {  
    85.         return (x & y) | ((~x) & z);  
    86.     }  
    87.   
    88.     private long G(long x, long y, long z) {  
    89.         return (x & z) | (y & (~z));  
    90.     }  
    91.   
    92.     private long H(long x, long y, long z) {  
    93.         return x ^ y ^ z;  
    94.     }  
    95.   
    96.     private long I(long x, long y, long z) {  
    97.         return y ^ (x | (~z));  
    98.     }  
    99.   
    100.     private long FF(long a, long b, long c, long d, long x, long s, long ac) {  
    101.         a += F(b, c, d) + x + ac;  
    102.         a = ((int) a << s) | ((int) a >>> (32 - s));  
    103.         a += b;  
    104.         return a;  
    105.     }  
    106.   
    107.     private long GG(long a, long b, long c, long d, long x, long s, long ac) {  
    108.         a += G(b, c, d) + x + ac;  
    109.         a = ((int) a << s) | ((int) a >>> (32 - s));  
    110.         a += b;  
    111.         return a;  
    112.     }  
    113.   
    114.     private long HH(long a, long b, long c, long d, long x, long s, long ac) {  
    115.         a += H(b, c, d) + x + ac;  
    116.         a = ((int) a << s) | ((int) a >>> (32 - s));  
    117.         a += b;  
    118.         return a;  
    119.     }  
    120.   
    121.     private long II(long a, long b, long c, long d, long x, long s, long ac) {  
    122.         a += I(b, c, d) + x + ac;  
    123.         a = ((int) a << s) | ((int) a >>> (32 - s));  
    124.         a += b;  
    125.         return a;  
    126.     }  
    127.   
    128.     private void md5Update(byte[] inbuf, int inputLen) {  
    129.         int i, index, partLen;  
    130.         byte[] block = new byte[64];  
    131.         index = (int) (count[0] >>> 3) & 0x3F;  
    132.         // /* Update number of bits */  
    133.         if ((count[0] += (inputLen << 3)) < (inputLen << 3))  
    134.             count[1]++;  
    135.         count[1] += (inputLen >>> 29);  
    136.         partLen = 64 - index;  
    137.         // Transform as many times as possible.  
    138.         if (inputLen >= partLen) {  
    139.             md5Memcpy(buffer, inbuf, index, 0, partLen);  
    140.             md5Transform(buffer);  
    141.             for (i = partLen; i + 63 < inputLen; i += 64) {  
    142.                 md5Memcpy(block, inbuf, 0, i, 64);  
    143.                 md5Transform(block);  
    144.             }  
    145.             index = 0;  
    146.         } else  
    147.             i = 0;  
    148.         // /* Buffer remaining input */  
    149.         md5Memcpy(buffer, inbuf, index, i, inputLen - i);  
    150.     }  
    151.   
    152.     private void md5Final() {  
    153.         byte[] bits = new byte[8];  
    154.         int index, padLen;  
    155.         // /* Save number of bits */  
    156.         Encode(bits, count, 8);  
    157.         // /* Pad out to 56 mod 64.  
    158.         index = (int) (count[0] >>> 3) & 0x3f;  
    159.         padLen = (index < 56) ? (56 - index) : (120 - index);  
    160.         md5Update(PADDING, padLen);  
    161.         // /* Append length (before padding) */  
    162.         md5Update(bits, 8);  
    163.         // /* Store state in digest */  
    164.         Encode(digest, state, 16);  
    165.     }  
    166.   
    167.     private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,  
    168.             int len) {  
    169.         int i;  
    170.         for (i = 0; i < len; i++)  
    171.             output[outpos + i] = input[inpos + i];  
    172.     }  
    173.   
    174.     private void md5Transform(byte block[]) {  
    175.         long a = state[0], b = state[1], c = state[2], d = state[3];  
    176.         long[] x = new long[16];  
    177.         Decode(x, block, 64);  
    178.         /* Round 1 */  
    179.         a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */  
    180.         d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */  
    181.         c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */  
    182.         b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */  
    183.         a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */  
    184.         d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */  
    185.         c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */  
    186.         b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */  
    187.         a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */  
    188.         d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */  
    189.         c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */  
    190.         b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */  
    191.         a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */  
    192.         d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */  
    193.         c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */  
    194.         b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */  
    195.         /* Round 2 */  
    196.         a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */  
    197.         d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */  
    198.         c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */  
    199.         b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */  
    200.         a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */  
    201.         d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */  
    202.         c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */  
    203.         b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */  
    204.         a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */  
    205.         d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */  
    206.         c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */  
    207.         b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */  
    208.         a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */  
    209.         d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */  
    210.         c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */  
    211.         b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */  
    212.         /* Round 3 */  
    213.         a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */  
    214.         d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */  
    215.         c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */  
    216.         b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */  
    217.         a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */  
    218.         d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */  
    219.         c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */  
    220.         b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */  
    221.         a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */  
    222.         d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */  
    223.         c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */  
    224.         b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */  
    225.         a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */  
    226.         d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */  
    227.         c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */  
    228.         b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */  
    229.         /* Round 4 */  
    230.         a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */  
    231.         d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */  
    232.         c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */  
    233.         b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */  
    234.         a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */  
    235.         d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */  
    236.         c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */  
    237.         b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */  
    238.         a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */  
    239.         d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */  
    240.         c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */  
    241.         b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */  
    242.         a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */  
    243.         d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */  
    244.         c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */  
    245.         b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */  
    246.         state[0] += a;  
    247.         state[1] += b;  
    248.         state[2] += c;  
    249.         state[3] += d;  
    250.     }  
    251.   
    252.     private void Encode(byte[] output, long[] input, int len) {  
    253.         int i, j;  
    254.         for (i = 0, j = 0; j < len; i++, j += 4) {  
    255.             output[j] = (byte) (input[i] & 0xffL);  
    256.             output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);  
    257.             output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);  
    258.             output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);  
    259.         }  
    260.     }  
    261.   
    262.     private void Decode(long[] output, byte[] input, int len) {  
    263.         int i, j;  
    264.         for (i = 0, j = 0; j < len; i++, j += 4)  
    265.             output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)  
    266.                     | (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);  
    267.         return;  
    268.     }  
    269.   
    270.     public static long b2iu(byte b) {  
    271.         return b < 0 ? b & 0x7F + 128 : b;  
    272.     }  
    273.       
    274.     public static String byteHEX(byte ib) {  
    275.         char[] Digit = { '0''1''2''3''4''5''6''7''8''9''A',  
    276.                 'B''C''D''E''F' };  
    277.         char[] ob = new char[2];  
    278.         ob[0] = Digit[(ib >>> 4) & 0X0F];  
    279.         ob[1] = Digit[ib & 0X0F];  
    280.         String s = new String(ob);  
    281.         return s;  
    282.     }  
    283. }  

     文件上传工具类 UploadUtil.java

    [java]  view plain  copy
    1. package com.util;  
    2.   
    3. import java.io.BufferedInputStream;  
    4. import java.io.BufferedOutputStream;  
    5. import java.io.File;  
    6. import java.io.FileInputStream;  
    7. import java.io.FileOutputStream;  
    8. import java.io.InputStream;  
    9. import java.io.OutputStream;  
    10. import java.util.Calendar;  
    11.   
    12. /** 
    13.  * 文件上传工具类 
    14.  * 
    15.  */  
    16. public class UploadUtil {  
    17.     private static final int BUFFER_SIZE = 16 * 1024;  
    18.     //保存图片  
    19.     public static synchronized void copy(File src, File newFile) {  
    20.           
    21.         try {  
    22.             InputStream is = null;  
    23.             OutputStream os = null;  
    24.             try {  
    25.                 is = new BufferedInputStream(new FileInputStream(src),  
    26.                         BUFFER_SIZE);  
    27.                 os = new BufferedOutputStream(new FileOutputStream(newFile),  
    28.                         BUFFER_SIZE);  
    29.                 byte[] buffer = new byte[BUFFER_SIZE];  
    30.                 while (is.read(buffer) > 0) {  
    31.                     os.write(buffer);  
    32.                 }  
    33.             } finally {  
    34.                 if (null != is) {  
    35.                     is.close();  
    36.                 }  
    37.                 if (null != os) {  
    38.                     os.close();  
    39.                 }  
    40.             }  
    41.         } catch (Exception e) {  
    42.             e.printStackTrace();  
    43.         }  
    44.     }  
    45.   
    46.     /** 
    47.      * 返回 年号+月号+天+时+分+秒+随机码 
    48.      * @return 
    49.      */  
    50.     @SuppressWarnings("static-access")  
    51.     public static synchronized String getTime() {  
    52.         Calendar calendar = Calendar.getInstance();  
    53.         String year = calendar.get(calendar.YEAR) + "";  
    54.         String month = (calendar.get(calendar.MONTH) + 1) + "";  
    55.         String day = calendar.get(calendar.DAY_OF_MONTH) + "";  
    56.         String hour = calendar.get(calendar.HOUR_OF_DAY) + "";  
    57.         String minute = calendar.get(calendar.MINUTE) + "";  
    58.         String second = calendar.get(calendar.SECOND) + "";  
    59.         String milliSecond = calendar.get(calendar.MILLISECOND) + "";  
    60.         int r = (int)(Math.random()*100000);  
    61.         String random = String.valueOf(r);  
    62.         return year + month + day + hour + minute + second + milliSecond + random+"a";  
    63.     }  
    64.   

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值