因为欣赏所以转载 原文地址 http://wuhaidong.iteye.com/blog/1668689

 

  • package com.common.file;  
  •   
  • import java.io.File;  
  • import java.io.FileInputStream;  
  • import java.io.FileNotFoundException;  
  • import java.io.FileOutputStream;  
  • import java.io.IOException;  
  • import java.io.InputStream;  
  • import java.io.OutputStream;  
  • import java.text.DateFormat;  
  • import java.util.Date;  
  • import java.util.Iterator;  
  •   
  • import javax.swing.text.html.HTMLDocument.HTMLReader.FormAction;  
  •   
  • /** 
  •  *  
  •  * 功能描述: 
  •  *  
  •  * @author Administrator 
  •  * @Date Jul 19, 2008 
  •  * @Time 9:46:11 AM 
  •  * @version 1.0 
  •  */  
  • public class FileUtil {  
  •   
  •     /** 
  •      * 功能描述:列出某文件夹及其子文件夹下面的文件,并可根据扩展名过滤 
  •      *  
  •      * @param path 
  •      *            文件夹 
  •      */  
  •     public static void list(File path) {  
  •         if (!path.exists()) {  
  •             System.out.println("文件名称不存在!");  
  •         } else {  
  •             if (path.isFile()) {  
  •                 if (path.getName().toLowerCase().endsWith(".pdf")  
  •                         || path.getName().toLowerCase().endsWith(".doc")  
  •                         || path.getName().toLowerCase().endsWith(".chm")  
  •                         || path.getName().toLowerCase().endsWith(".html")  
  •                         || path.getName().toLowerCase().endsWith(".htm")) {// 文件格式  
  •                     System.out.println(path);  
  •                     System.out.println(path.getName());  
  •                 }  
  •             } else {  
  •                 File[] files = path.listFiles();  
  •                 for (int i = 0; i < files.length; i++) {  
  •                     list(files[i]);  
  •                 }  
  •             }  
  •         }  
  •     }  
  •   
  •     /** 
  •      * 功能描述:拷贝一个目录或者文件到指定路径下,即把源文件拷贝到目标文件路径下 
  •      *  
  •      * @param source 
  •      *            源文件 
  •      * @param target 
  •      *            目标文件路径 
  •      * @return void 
  •      */  
  •     public static void copy(File source, File target) {  
  •         File tarpath = new File(target, source.getName());  
  •         if (source.isDirectory()) {  
  •             tarpath.mkdir();  
  •             File[] dir = source.listFiles();  
  •             for (int i = 0; i < dir.length; i++) {  
  •                 copy(dir[i], tarpath);  
  •             }  
  •         } else {  
  •             try {  
  •                 InputStream is = new FileInputStream(source); // 用于读取文件的原始字节流  
  •                 OutputStream os = new FileOutputStream(tarpath); // 用于写入文件的原始字节的流  
  •                 byte[] buf = new byte[1024];// 存储读取数据的缓冲区大小  
  •                 int len = 0;  
  •                 while ((len = is.read(buf)) != -1) {  
  •                     os.write(buf, 0, len);  
  •                 }  
  •                 is.close();  
  •                 os.close();  
  •             } catch (FileNotFoundException e) {  
  •                 e.printStackTrace();  
  •             } catch (IOException e) {  
  •                 e.printStackTrace();  
  •             }  
  •         }  
  •     }  
  •   
  •     /** 
  •      * @param args 
  •      */  
  •     public static void main(String[] args) {  
  •         // TODO Auto-generated method stub  
  •         File file = new File("F:\\Tomcat");  
  •         list(file);  
  •         Date myDate = new Date();   
  •         DateFormat df = DateFormat.getDateInstance();  
  •         System.out.println(df.format(myDate));   
  •     }  
  •   
  • }  
  •  

     

     

    Java代码  java经典代码
    1. package com.common.string;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.LinkedHashSet;  
    5. import java.util.Set;  
    6. import java.util.regex.Matcher;  
    7. import java.util.regex.Pattern;  
    8.   
    9. /** 
    10.  * 功能描述:关于字符串的一些实用操作 
    11.  *  
    12.  * @author Administrator 
    13.  * @Date Jul 18, 2008 
    14.  * @Time 2:19:47 PM 
    15.  * @version 1.0 
    16.  */  
    17. public class StringUtil {  
    18.   
    19.     /** 
    20.      * 功能描述:分割字符串 
    21.      *  
    22.      * @param str 
    23.      *            String 原始字符串 
    24.      * @param splitsign 
    25.      *            String 分隔符 
    26.      * @return String[] 分割后的字符串数组 
    27.      */  
    28.     @SuppressWarnings("unchecked")  
    29.     public static String[] split(String str, String splitsign) {  
    30.         int index;  
    31.         if (str == null || splitsign == null) {  
    32.             return null;  
    33.         }  
    34.         ArrayList al = new ArrayList();  
    35.         while ((index = str.indexOf(splitsign)) != -1) {  
    36.             al.add(str.substring(0, index));  
    37.             str = str.substring(index + splitsign.length());  
    38.         }  
    39.         al.add(str);  
    40.         return (String[]) al.toArray(new String[0]);  
    41.     }  
    42.   
    43.     /** 
    44.      * 功能描述:替换字符串 
    45.      *  
    46.      * @param from 
    47.      *            String 原始字符串 
    48.      * @param to 
    49.      *            String 目标字符串 
    50.      * @param source 
    51.      *            String 母字符串 
    52.      * @return String 替换后的字符串 
    53.      */  
    54.     public static String replace(String from, String to, String source) {  
    55.         if (source == null || from == null || to == null)  
    56.             return null;  
    57.         StringBuffer str = new StringBuffer("");  
    58.         int index = -1;  
    59.         while ((index = source.indexOf(from)) != -1) {  
    60.             str.append(source.substring(0, index) + to);  
    61.             source = source.substring(index + from.length());  
    62.             index = source.indexOf(from);  
    63.         }  
    64.         str.append(source);  
    65.         return str.toString();  
    66.     }  
    67.   
    68.     /** 
    69.      * 替换字符串,能能够在HTML页面上直接显示(替换双引号和小于号) 
    70.      *  
    71.      * @param str 
    72.      *            String 原始字符串 
    73.      * @return String 替换后的字符串 
    74.      */  
    75.     public static String htmlencode(String str) {  
    76.         if (str == null) {  
    77.             return null;  
    78.         }  
    79.         return replace("\"", "&quot;", replace("<", "&lt;", str));  
    80.     }  
    81.   
    82.     /** 
    83.      * 替换字符串,将被编码的转换成原始码(替换成双引号和小于号) 
    84.      *  
    85.      * @param str 
    86.      *            String 
    87.      * @return String 
    88.      */  
    89.     public static String htmldecode(String str) {  
    90.         if (str == null) {  
    91.             return null;  
    92.         }  
    93.   
    94.         return replace("&quot;", "\"", replace("&lt;", "<", str));  
    95.     }  
    96.   
    97.     private static final String _BR = "<br/>";  
    98.   
    99.     /** 
    100.      * 功能描述:在页面上直接显示文本内容,替换小于号,空格,回车,TAB 
    101.      *  
    102.      * @param str 
    103.      *            String 原始字符串 
    104.      * @return String 替换后的字符串 
    105.      */  
    106.     public static String htmlshow(String str) {  
    107.         if (str == null) {  
    108.             return null;  
    109.         }  
    110.   
    111.         str = replace("<", "&lt;", str);  
    112.         str = replace(" ", "&nbsp;", str);  
    113.         str = replace("\r\n", _BR, str);  
    114.         str = replace("\n", _BR, str);  
    115.         str = replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;", str);  
    116.         return str;  
    117.     }  
    118.   
    119.     /** 
    120.      * 功能描述:返回指定字节长度的字符串 
    121.      *  
    122.      * @param str 
    123.      *            String 字符串 
    124.      * @param length 
    125.      *            int 指定长度 
    126.      * @return String 返回的字符串 
    127.      */  
    128.     public static String toLength(String str, int length) {  
    129.         if (str == null) {  
    130.             return null;  
    131.         }  
    132.         if (length <= 0) {  
    133.             return "";  
    134.         }  
    135.         try {  
    136.             if (str.getBytes("GBK").length <= length) {  
    137.                 return str;  
    138.             }  
    139.         } catch (Exception e) {  
    140.         }  
    141.         StringBuffer buff = new StringBuffer();  
    142.   
    143.         int index = 0;  
    144.         char c;  
    145.         length -= 3;  
    146.         while (length > 0) {  
    147.             c = str.charAt(index);  
    148.             if (c < 128) {  
    149.                 length--;  
    150.             } else {  
    151.                 length--;  
    152.                 length--;  
    153.             }  
    154.             buff.append(c);  
    155.             index++;  
    156.         }  
    157.         buff.append("...");  
    158.         return buff.toString();  
    159.     }  
    160.   
    161.     /** 
    162.      * 功能描述:判断是否为整数 
    163.      *  
    164.      * @param str 
    165.      *            传入的字符串 
    166.      * @return 是整数返回true,否则返回false 
    167.      */  
    168.     public static boolean isInteger(String str) {  
    169.         Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$");  
    170.         return pattern.matcher(str).matches();  
    171.     }  
    172.   
    173.     /** 
    174.      * 判断是否为浮点数,包括double和float 
    175.      *  
    176.      * @param str 
    177.      *            传入的字符串 
    178.      * @return 是浮点数返回true,否则返回false 
    179.      */  
    180.     public static boolean isDouble(String str) {  
    181.         Pattern pattern = Pattern.compile("^[-\\+]?\\d+\\.\\d+$");  
    182.         return pattern.matcher(str).matches();  
    183.     }  
    184.   
    185.     /** 
    186.      * 判断是不是合法字符 c 要判断的字符 
    187.      */  
    188.     public static boolean isLetter(String str) {  
    189.         if (str == null || str.length() < 0) {  
    190.             return false;  
    191.         }  
    192.         Pattern pattern = Pattern.compile("[\\w\\.-_]*");  
    193.         return pattern.matcher(str).matches();  
    194.     }  
    195.   
    196.     /** 
    197.      * 从指定的字符串中提取Email content 指定的字符串 
    198.      *  
    199.      * @param content 
    200.      * @return 
    201.      */  
    202.     public static String parse(String content) {  
    203.         String email = null;  
    204.         if (content == null || content.length() < 1) {  
    205.             return email;  
    206.         }  
    207.         // 找出含有@  
    208.         int beginPos;  
    209.         int i;  
    210.         String token = "@";  
    211.         String preHalf = "";  
    212.         String sufHalf = "";  
    213.   
    214.         beginPos = content.indexOf(token);  
    215.         if (beginPos > -1) {  
    216.             // 前项扫描  
    217.             String s = null;  
    218.             i = beginPos;  
    219.             while (i > 0) {  
    220.                 s = content.substring(i - 1, i);  
    221.                 if (isLetter(s))  
    222.                     preHalf = s + preHalf;  
    223.                 else  
    224.                     break;  
    225.                 i--;  
    226.             }  
    227.             // 后项扫描  
    228.             i = beginPos + 1;  
    229.             while (i < content.length()) {  
    230.                 s = content.substring(i, i + 1);  
    231.                 if (isLetter(s))  
    232.                     sufHalf = sufHalf + s;  
    233.                 else  
    234.                     break;  
    235.                 i++;  
    236.             }  
    237.             // 判断合法性  
    238.             email = preHalf + "@" + sufHalf;  
    239.             if (isEmail(email)) {  
    240.                 return email;  
    241.             }  
    242.         }  
    243.         return null;  
    244.     }  
    245.   
    246.     /** 
    247.      * 功能描述:判断输入的字符串是否符合Email样式. 
    248.      *  
    249.      * @param str 
    250.      *            传入的字符串 
    251.      * @return 是Email样式返回true,否则返回false 
    252.      */  
    253.     public static boolean isEmail(String email) {  
    254.         if (email == null || email.length() < 1 || email.length() > 256) {  
    255.             return false;  
    256.         }  
    257.         Pattern pattern = Pattern  
    258.                 .compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");  
    259.         return pattern.matcher(email).matches();  
    260.     }  
    261.   
    262.     /** 
    263.      * 功能描述:判断输入的字符串是否为纯汉字 
    264.      *  
    265.      * @param str 
    266.      *            传入的字符窜 
    267.      * @return 如果是纯汉字返回true,否则返回false 
    268.      */  
    269.     public static boolean isChinese(String str) {  
    270.         Pattern pattern = Pattern.compile("[\u0391-\uFFE5]+$");  
    271.         return pattern.matcher(str).matches();  
    272.     }  
    273.   
    274.     /** 
    275.      * 功能描述:是否为空白,包括null和"" 
    276.      *  
    277.      * @param str 
    278.      * @return 
    279.      */  
    280.     public static boolean isBlank(String str) {  
    281.         return str == null || str.trim().length() == 0;  
    282.     }  
    283.   
    284.     /** 
    285.      * 功能描述:判断是否为质数 
    286.      *  
    287.      * @param x 
    288.      * @return 
    289.      */  
    290.     public static boolean isPrime(int x) {  
    291.         if (x <= 7) {  
    292.             if (x == 2 || x == 3 || x == 5 || x == 7)  
    293.                 return true;  
    294.         }  
    295.         int c = 7;  
    296.         if (x % 2 == 0)  
    297.             return false;  
    298.         if (x % 3 == 0)  
    299.             return false;  
    300.         if (x % 5 == 0)  
    301.             return false;  
    302.         int end = (int) Math.sqrt(x);  
    303.         while (c <= end) {  
    304.             if (x % c == 0) {  
    305.                 return false;  
    306.             }  
    307.             c += 4;  
    308.             if (x % c == 0) {  
    309.                 return false;  
    310.             }  
    311.             c += 2;  
    312.             if (x % c == 0) {  
    313.                 return false;  
    314.             }  
    315.             c += 4;  
    316.             if (x % c == 0) {  
    317.                 return false;  
    318.             }  
    319.             c += 2;  
    320.             if (x % c == 0) {  
    321.                 return false;  
    322.             }  
    323.             c += 4;  
    324.             if (x % c == 0) {  
    325.                 return false;  
    326.             }  
    327.             c += 6;  
    328.             if (x % c == 0) {  
    329.                 return false;  
    330.             }  
    331.             c += 2;  
    332.             if (x % c == 0) {  
    333.                 return false;  
    334.             }  
    335.             c += 6;  
    336.         }  
    337.         return true;  
    338.     }  
    339.   
    340.     /** 
    341.      * 功能描述:人民币转成大写 
    342.      *  
    343.      * @param str 
    344.      *            数字字符串 
    345.      * @return String 人民币转换成大写后的字符串 
    346.      */  
    347.     public static String hangeToBig(String str) {  
    348.         double value;  
    349.         try {  
    350.             value = Double.parseDouble(str.trim());  
    351.         } catch (Exception e) {  
    352.             return null;  
    353.         }  
    354.         char[] hunit = { '拾', '佰', '仟' }; // 段内位置表示  
    355.         char[] vunit = { '万', '亿' }; // 段名表示  
    356.         char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; // 数字表示  
    357.         long midVal = (long) (value * 100); // 转化成整形  
    358.         String valStr = String.valueOf(midVal); // 转化成字符串  
    359.   
    360.         String head = valStr.substring(0, valStr.length() - 2); // 取整数部分  
    361.         String rail = valStr.substring(valStr.length() - 2); // 取小数部分  
    362.   
    363.         String prefix = ""; // 整数部分转化的结果  
    364.         String suffix = ""; // 小数部分转化的结果  
    365.         // 处理小数点后面的数  
    366.         if (rail.equals("00")) { // 如果小数部分为0  
    367.             suffix = "整";  
    368.         } else {  
    369.             suffix = digit[rail.charAt(0) - '0'] + "角"  
    370.                     + digit[rail.charAt(1) - '0'] + "分"; // 否则把角分转化出来  
    371.         }  
    372.         // 处理小数点前面的数  
    373.         char[] chDig = head.toCharArray(); // 把整数部分转化成字符数组  
    374.         char zero = '0'; // 标志'0'表示出现过0  
    375.         byte zeroSerNum = 0; // 连续出现0的次数  
    376.         for (int i = 0; i < chDig.length; i++) { // 循环处理每个数字  
    377.             int idx = (chDig.length - i - 1) % 4; // 取段内位置  
    378.             int vidx = (chDig.length - i - 1) / 4; // 取段位置  
    379.             if (chDig[i] == '0') { // 如果当前字符是0  
    380.                 zeroSerNum++; // 连续0次数递增  
    381.                 if (zero == '0') { // 标志  
    382.                     zero = digit[0];  
    383.                 } else if (idx == 0 && vidx > 0 && zeroSerNum < 4) {  
    384.                     prefix += vunit[vidx - 1];  
    385.                     zero = '0';  
    386.                 }  
    387.                 continue;  
    388.             }  
    389.             zeroSerNum = 0; // 连续0次数清零  
    390.             if (zero != '0') { // 如果标志不为0,则加上,例如万,亿什么的  
    391.                 prefix += zero;  
    392.                 zero = '0';  
    393.             }  
    394.             prefix += digit[chDig[i] - '0']; // 转化该数字表示  
    395.             if (idx > 0)  
    396.                 prefix += hunit[idx - 1];  
    397.             if (idx == 0 && vidx > 0) {  
    398.                 prefix += vunit[vidx - 1]; // 段结束位置应该加上段名如万,亿  
    399.             }  
    400.         }  
    401.   
    402.         if (prefix.length() > 0)  
    403.             prefix += '圆'; // 如果整数部分存在,则有圆的字样  
    404.         return prefix + suffix; // 返回正确表示  
    405.     }  
    406.   
    407.     /** 
    408.      * 功能描述:去掉字符串中重复的子字符串 
    409.      *  
    410.      * @param str 
    411.      *            原字符串,如果有子字符串则用空格隔开以表示子字符串 
    412.      * @return String 返回去掉重复子字符串后的字符串 
    413.      */  
    414.     @SuppressWarnings("unused")  
    415.     private static String removeSameString(String str) {  
    416.         Set<String> mLinkedSet = new LinkedHashSet<String>();// set集合的特征:其子集不可以重复  
    417.         String[] strArray = str.split(" ");// 根据空格(正则表达式)分割字符串  
    418.         StringBuffer sb = new StringBuffer();  
    419.   
    420.         for (int i = 0; i < strArray.length; i++) {  
    421.             if (!mLinkedSet.contains(strArray[i])) {  
    422.                 mLinkedSet.add(strArray[i]);  
    423.                 sb.append(strArray[i] + " ");  
    424.             }  
    425.         }  
    426.         System.out.println(mLinkedSet);  
    427.         return sb.toString();  
    428.     }  
    429.   
    430.     /** 
    431.      * 功能描述:过滤特殊字符 
    432.      *  
    433.      * @param src 
    434.      * @return 
    435.      */  
    436.     public static String encoding(String src) {  
    437.         if (src == null)  
    438.             return "";  
    439.         StringBuilder result = new StringBuilder();  
    440.         if (src != null) {  
    441.             src = src.trim();  
    442.             for (int pos = 0; pos < src.length(); pos++) {  
    443.                 switch (src.charAt(pos)) {  
    444.                 case '\"':  
    445.                     result.append("&quot;");  
    446.                     break;  
    447.                 case '<':  
    448.                     result.append("&lt;");  
    449.                     break;  
    450.                 case '>':  
    451.                     result.append("&gt;");  
    452.                     break;  
    453.                 case '\'':  
    454.                     result.append("&apos;");  
    455.                     break;  
    456.                 case '&':  
    457.                     result.append("&amp;");  
    458.                     break;  
    459.                 case '%':  
    460.                     result.append("&pc;");  
    461.                     break;  
    462.                 case '_':  
    463.                     result.append("&ul;");  
    464.                     break;  
    465.                 case '#':  
    466.                     result.append("&shap;");  
    467.                     break;  
    468.                 case '?':  
    469.                     result.append("&ques;");  
    470.                     break;  
    471.                 default:  
    472.                     result.append(src.charAt(pos));  
    473.                     break;  
    474.                 }  
    475.             }  
    476.         }  
    477.         return result.toString();  
    478.     }  
    479.   
    480.     /** 
    481.      * 功能描述:判断是不是合法的手机号码 
    482.      *  
    483.      * @param handset 
    484.      * @return boolean 
    485.      */  
    486.     public static boolean isHandset(String handset) {  
    487.         try {  
    488.             String regex = "^1[\\d]{10}$";  
    489.             Pattern pattern = Pattern.compile(regex);  
    490.             Matcher matcher = pattern.matcher(handset);  
    491.             return matcher.matches();  
    492.   
    493.         } catch (RuntimeException e) {  
    494.             return false;  
    495.         }  
    496.     }  
    497.   
    498.     /** 
    499.      * 功能描述:反过滤特殊字符 
    500.      *  
    501.      * @param src 
    502.      * @return 
    503.      */  
    504.     public static String decoding(String src) {  
    505.         if (src == null)  
    506.             return "";  
    507.         String result = src;  
    508.         result = result.replace("&quot;", "\"").replace("&apos;", "\'");  
    509.         result = result.replace("&lt;", "<").replace("&gt;", ">");  
    510.         result = result.replace("&amp;", "&");  
    511.         result = result.replace("&pc;", "%").replace("&ul", "_");  
    512.         result = result.replace("&shap;", "#").replace("&ques", "?");  
    513.         return result;  
    514.     }  
    515.   
    516.     /** 
    517.      * @param args 
    518.      */  
    519.     public static void main(String[] args) {  
    520.          String source = "abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg";  
    521.          String from = "efg";  
    522.          String to = "房贺威";  
    523.          System.out.println("在字符串source中,用to替换from,替换结果为:"  
    524.          + replace(from, to, source));  
    525.          System.out.println("返回指定字节长度的字符串:"  
    526.          + toLength("abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg", 9));  
    527.          System.out.println("判断是否为整数:" + isInteger("+0"));  
    528.          System.out.println("判断是否为浮点数,包括double和float:" + isDouble("+0.36"));  
    529.          System.out.println("判断输入的字符串是否符合Email样式:" +  
    530.          isEmail("fhwbj@163.com"));  
    531.          System.out.println("判断输入的字符串是否为纯汉字:" + isChinese("你好!"));  
    532.          System.out.println("判断输入的数据是否是质数:" + isPrime(12));  
    533.          System.out.println("人民币转换成大写:" + hangeToBig("10019658"));  
    534.          System.out.println("去掉字符串中重复的子字符串:" + removeSameString("100 100 9658"));  
    535.          System.out.println("过滤特殊字符:" + encoding("100\"s<>fdsd100 9658"));  
    536.          System.out.println("判断是不是合法的手机号码:" + isHandset("15981807340"));  
    537.          System.out.println("从字符串中取值Email:" + parse("159818 fwhbj@163.com07340"));  
    538.     }  
    539. }  

     

     

     

    Java代码  java经典代码
    1. package com.common.time;  
    2.   
    3. import java.text.DateFormat;  
    4. import java.text.SimpleDateFormat;  
    5. import java.util.Calendar;  
    6. import java.util.Date;  
    7.   
    8. /** 
    9.  *  
    10.  * 功能描述: 
    11.  *  
    12.  * @author Administrator 
    13.  * @Date Jul 19, 2008 
    14.  * @Time 9:47:53 AM 
    15.  * @version 1.0 
    16.  */  
    17. public class DateUtil {  
    18.   
    19.     public static Date date = null;  
    20.   
    21.     public static DateFormat dateFormat = null;  
    22.   
    23.     public static Calendar calendar = null;  
    24.   
    25.     /** 
    26.      * 功能描述:格式化日期 
    27.      *  
    28.      * @param dateStr 
    29.      *            String 字符型日期 
    30.      * @param format 
    31.      *            String 格式 
    32.      * @return Date 日期 
    33.      */  
    34.     public static Date parseDate(String dateStr, String format) {  
    35.         try {  
    36.             dateFormat = new SimpleDateFormat(format);  
    37.             String dt = dateStr.replaceAll("-", "/");  
    38.             if ((!dt.equals("")) && (dt.length() < format.length())) {  
    39.                 dt += format.substring(dt.length()).replaceAll("[YyMmDdHhSs]",  
    40.                         "0");  
    41.             }  
    42.             date = (Date) dateFormat.parse(dt);  
    43.         } catch (Exception e) {  
    44.         }  
    45.         return date;  
    46.     }  
    47.   
    48.     /** 
    49.      * 功能描述:格式化日期 
    50.      *  
    51.      * @param dateStr 
    52.      *            String 字符型日期:YYYY-MM-DD 格式 
    53.      * @return Date 
    54.      */  
    55.     public static Date parseDate(String dateStr) {  
    56.         return parseDate(dateStr, "yyyy/MM/dd");  
    57.     }  
    58.   
    59.     /** 
    60.      * 功能描述:格式化输出日期 
    61.      *  
    62.      * @param date 
    63.      *            Date 日期 
    64.      * @param format 
    65.      *            String 格式 
    66.      * @return 返回字符型日期 
    67.      */  
    68.     public static String format(Date date, String format) {  
    69.         String result = "";  
    70.         try {  
    71.             if (date != null) {  
    72.                 dateFormat = new SimpleDateFormat(format);  
    73.                 result = dateFormat.format(date);  
    74.             }  
    75.         } catch (Exception e) {  
    76.         }  
    77.         return result;  
    78.     }  
    79.   
    80.     /** 
    81.      * 功能描述: 
    82.      *  
    83.      * @param date 
    84.      *            Date 日期 
    85.      * @return 
    86.      */  
    87.     public static String format(Date date) {  
    88.         return format(date, "yyyy/MM/dd");  
    89.     }  
    90.   
    91.     /** 
    92.      * 功能描述:返回年份 
    93.      *  
    94.      * @param date 
    95.      *            Date 日期 
    96.      * @return 返回年份 
    97.      */  
    98.     public static int getYear(Date date) {  
    99.         calendar = Calendar.getInstance();  
    100.         calendar.setTime(date);  
    101.         return calendar.get(Calendar.YEAR);  
    102.     }  
    103.   
    104.     /** 
    105.      * 功能描述:返回月份 
    106.      *  
    107.      * @param date 
    108.      *            Date 日期 
    109.      * @return 返回月份 
    110.      */  
    111.     public static int getMonth(Date date) {  
    112.         calendar = Calendar.getInstance();  
    113.         calendar.setTime(date);  
    114.         return calendar.get(Calendar.MONTH) + 1;  
    115.     }  
    116.   
    117.     /** 
    118.      * 功能描述:返回日份 
    119.      *  
    120.      * @param date 
    121.      *            Date 日期 
    122.      * @return 返回日份 
    123.      */  
    124.     public static int getDay(Date date) {  
    125.         calendar = Calendar.getInstance();  
    126.         calendar.setTime(date);  
    127.         return calendar.get(Calendar.DAY_OF_MONTH);  
    128.     }  
    129.   
    130.     /** 
    131.      * 功能描述:返回小时 
    132.      *  
    133.      * @param date 
    134.      *            日期 
    135.      * @return 返回小时 
    136.      */  
    137.     public static int getHour(Date date) {  
    138.         calendar = Calendar.getInstance();  
    139.         calendar.setTime(date);  
    140.         return calendar.get(Calendar.HOUR_OF_DAY);  
    141.     }  
    142.   
    143.     /** 
    144.      * 功能描述:返回分钟 
    145.      *  
    146.      * @param date 
    147.      *            日期 
    148.      * @return 返回分钟 
    149.      */  
    150.     public static int getMinute(Date date) {  
    151.         calendar = Calendar.getInstance();  
    152.         calendar.setTime(date);  
    153.         return calendar.get(Calendar.MINUTE);  
    154.     }  
    155.   
    156.     /** 
    157.      * 返回秒钟 
    158.      *  
    159.      * @param date 
    160.      *            Date 日期 
    161.      * @return 返回秒钟 
    162.      */  
    163.     public static int getSecond(Date date) {  
    164.         calendar = Calendar.getInstance();  
    165.         calendar.setTime(date);  
    166.         return calendar.get(Calendar.SECOND);  
    167.     }  
    168.   
    169.     /** 
    170.      * 功能描述:返回毫秒 
    171.      *  
    172.      * @param date 
    173.      *            日期 
    174.      * @return 返回毫秒 
    175.      */  
    176.     public static long getMillis(Date date) {  
    177.         calendar = Calendar.getInstance();  
    178.         calendar.setTime(date);  
    179.         return calendar.getTimeInMillis();  
    180.     }  
    181.   
    182.     /** 
    183.      * 功能描述:返回字符型日期 
    184.      *  
    185.      * @param date 
    186.      *            日期 
    187.      * @return 返回字符型日期 yyyy/MM/dd 格式 
    188.      */  
    189.     public static String getDate(Date date) {  
    190.         return format(date, "yyyy/MM/dd");  
    191.     }  
    192.   
    193.     /** 
    194.      * 功能描述:返回字符型时间 
    195.      *  
    196.      * @param date 
    197.      *            Date 日期 
    198.      * @return 返回字符型时间 HH:mm:ss 格式 
    199.      */  
    200.     public static String getTime(Date date) {  
    201.         return format(date, "HH:mm:ss");  
    202.     }  
    203.   
    204.     /** 
    205.      * 功能描述:返回字符型日期时间 
    206.      *  
    207.      * @param date 
    208.      *            Date 日期 
    209.      * @return 返回字符型日期时间 yyyy/MM/dd HH:mm:ss 格式 
    210.      */  
    211.     public static String getDateTime(Date date) {  
    212.         return format(date, "yyyy/MM/dd HH:mm:ss");  
    213.     }  
    214.   
    215.     /** 
    216.      * 功能描述:日期相加 
    217.      *  
    218.      * @param date 
    219.      *            Date 日期 
    220.      * @param day 
    221.      *            int 天数 
    222.      * @return 返回相加后的日期 
    223.      */  
    224.     public static Date addDate(Date date, int day) {  
    225.         calendar = Calendar.getInstance();  
    226.         long millis = getMillis(date) + ((long) day) * 24 * 3600 * 1000;  
    227.         calendar.setTimeInMillis(millis);  
    228.         return calendar.getTime();  
    229.     }  
    230.   
    231.     /** 
    232.      * 功能描述:日期相减 
    233.      *  
    234.      * @param date 
    235.      *            Date 日期 
    236.      * @param date1 
    237.      *            Date 日期 
    238.      * @return 返回相减后的日期 
    239.      */  
    240.     public static int diffDate(Date date, Date date1) {  
    241.         return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));  
    242.     }  
    243.   
    244.     /** 
    245.      * 功能描述:取得指定月份的第一天 
    246.      *  
    247.      * @param strdate 
    248.      *            String 字符型日期 
    249.      * @return String yyyy-MM-dd 格式 
    250.      */  
    251.     public static String getMonthBegin(String strdate) {  
    252.         date = parseDate(strdate);  
    253.         return format(date, "yyyy-MM") + "-01";  
    254.     }  
    255.   
    256.     /** 
    257.      * 功能描述:取得指定月份的最后一天 
    258.      *  
    259.      * @param strdate 
    260.      *            String 字符型日期 
    261.      * @return String 日期字符串 yyyy-MM-dd格式 
    262.      */  
    263.     public static String getMonthEnd(String strdate) {  
    264.         date = parseDate(getMonthBegin(strdate));  
    265.         calendar = Calendar.getInstance();  
    266.         calendar.setTime(date);  
    267.         calendar.add(Calendar.MONTH, 1);  
    268.         calendar.add(Calendar.DAY_OF_YEAR, -1);  
    269.         return formatDate(calendar.getTime());  
    270.     }  
    271.   
    272.     /** 
    273.      * 功能描述:常用的格式化日期 
    274.      *  
    275.      * @param date 
    276.      *            Date 日期 
    277.      * @return String 日期字符串 yyyy-MM-dd格式 
    278.      */  
    279.     public static String formatDate(Date date) {  
    280.         return formatDateByFormat(date, "yyyy-MM-dd");  
    281.     }  
    282.   
    283.     /** 
    284.      * 功能描述:以指定的格式来格式化日期 
    285.      *  
    286.      * @param date 
    287.      *            Date 日期 
    288.      * @param format 
    289.      *            String 格式 
    290.      * @return String 日期字符串 
    291.      */  
    292.     public static String formatDateByFormat(Date date, String format) {  
    293.         String result = "";  
    294.         if (date != null) {  
    295.             try {  
    296.                 SimpleDateFormat sdf = new SimpleDateFormat(format);  
    297.                 result = sdf.format(date);  
    298.             } catch (Exception ex) {  
    299.                 ex.printStackTrace();  
    300.             }  
    301.         }  
    302.         return result;  
    303.     }  
    304.   
    305.     public static void main(String[] args) {  
    306.         Date d = new Date();  
    307.         System.out.println(d.toString());  
    308.         System.out.println(formatDate(d).toString());  
    309.         System.out.println(getMonthBegin(formatDate(d).toString()));  
    310.         System.out.println(getMonthBegin("2008/07/19"));  
    311.         System.out.println(getMonthEnd("2008/07/19"));  
    312.         System.out.println(addDate(d,15).toString());  
    313.     }  
    314.   
    315. }  

     

     

     

    Java代码  java经典代码
    1. /*[java]一个压缩工具类 zip2008年05月28日 星期三 17:48/* 
    2. * Zip.java 
    3. * Created on 2008年4月2日, 下午2:20 
    4. * To change this template, choose Tools | Template Manager 
    5. * and open the template in the editor. 
    6. */  
    7.   
    8. package com.founder.mnp.utl;  
    9.   
    10. /** 
    11. * @author GuoJiale 
    12. */  
    13. import java.io.BufferedInputStream;  
    14. import java.io.BufferedOutputStream;  
    15. import java.io.File;  
    16. import java.io.FileInputStream;  
    17. import java.io.FileOutputStream;  
    18. import java.util.zip.ZipEntry;  
    19. import java.util.zip.ZipOutputStream;  
    20.   
    21.   
    22. public class ZipUtils {  
    23.     static final int BUFFER = 2048;  
    24.       
    25.     public static boolean zip( String[] filename ,String outname){  
    26.           
    27.         boolean test = true;  
    28.         try {  
    29.             BufferedInputStream origin = null;  
    30.             FileOutputStream dest = new FileOutputStream(outname);  
    31.             ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(  
    32.                     dest));  
    33.             byte data[] = new byte[BUFFER];  
    34.             // File f= new File("d:\\111\\");  
    35.             //   File files[] = f.listFiles();  
    36.               
    37.             for (int i = 0; i < filename.length; i++) {  
    38.                 File file = new File(filename[i]);  
    39.                 FileInputStream fi = new FileInputStream(file);  
    40.                 origin = new BufferedInputStream(fi, BUFFER);  
    41.                 ZipEntry entry = new ZipEntry(file.getName());  
    42.                 out.putNextEntry(entry);  
    43.                 int count;  
    44.                 while ((count = origin.read(data, 0, BUFFER)) != -1) {  
    45.                     out.write(data, 0, count);  
    46.                 }  
    47.                 origin.close();  
    48.             }  
    49.             out.close();  
    50.         } catch (Exception e) {  
    51.             test = false;  
    52.             e.printStackTrace();  
    53.         }  
    54.         return test;  
    55.     }  
    56.       
    57.       
    58.       
    59.     public static void main(String argv[]) {  
    60.         // File f= new File("d:\\111\\");  
    61.         String[] filenames = new String[]{"F:\\12.jpg"};  
    62.         zip(filenames,"F:/12.zip");  
    63.     }  
    64. }  
    65.    

     

     

     

    Java代码  java经典代码
    1. package com.test.test;  
    2.   
    3. /************************************************ 
    4. MD5 算法的Java Bean 
    5. Last Modified:10,Mar,2001 
    6.  *************************************************/  
    7. import java.io.File;  
    8. import java.io.FileInputStream;  
    9. import java.io.FileNotFoundException;  
    10. import java.io.IOException;  
    11. import java.math.BigInteger;  
    12. import java.security.MessageDigest;  
    13. import java.util.HashMap;  
    14. import java.util.Map;  
    15. import java.util.zip.CRC32;  
    16. import java.util.zip.CheckedInputStream;  
    17.   
    18. /************************************************* 
    19.  * md5 类实现了RSA Data Security, Inc.在提交给IETF 的RFC1321中的MD5 message-digest 算法。 
    20.  *************************************************/  
    21. public class MD5 {  
    22.     /* 
    23.      * 下面这些S11-S44实际上是一个44的矩阵,在原始的C实现中是用#define 实现的, 这里把它们实现成为static 
    24.      * final是表示了只读,切能在同一个进程空间内的多个 Instance间共享 
    25.      */  
    26.     private static final int S11 = 7;  
    27.     private static final int S12 = 12;  
    28.     private static final int S13 = 17;  
    29.     private static final int S14 = 22;  
    30.     private static final int S21 = 5;  
    31.     private static final int S22 = 9;  
    32.     private static final int S23 = 14;  
    33.     private static final int S24 = 20;  
    34.     private static final int S31 = 4;  
    35.     private static final int S32 = 11;  
    36.     private static final int S33 = 16;  
    37.     private static final int S34 = 23;  
    38.     private static final int S41 = 6;  
    39.     private static final int S42 = 10;  
    40.     private static final int S43 = 15;  
    41.     private static final int S44 = 21;  
    42.     private static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
    43.             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
    44.             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
    45.             0, 0, 0, 0, 0, 0, 0 };  
    46.     /* 
    47.      * 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中 被定义到MD5_CTX结构中 
    48.      */  
    49.     private long[] state = new long[4]; // state (ABCD)  
    50.     private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb  
    51.                                         // first)  
    52.     private byte[] buffer = new byte[64]; // input buffer  
    53.     /* 
    54.      * digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的 16进制ASCII表示. 
    55.      */  
    56.     private String digestHexStr;  
    57.     /* 
    58.      * digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值. 
    59.      */  
    60.     private byte[] digest = new byte[16];  
    61.   
    62.     /** 
    63.      * getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串 
    64.      * 返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的. 
    65.      */  
    66.     public String getMD5ofStr(String inbuf) {  
    67.         md5Init();  
    68.         md5Update(inbuf.getBytes(), inbuf.length());  
    69.         md5Final();  
    70.         digestHexStr = "";  
    71.         for (int i = 0; i < 16; i++) {  
    72.             digestHexStr += byteHEX(digest[i]);  
    73.         }  
    74.         return digestHexStr;  
    75.     }  
    76.   
    77.     // 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数  
    78.     public MD5() {  
    79.         md5Init();  
    80.         return;  
    81.     }  
    82.   
    83.     /* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */  
    84.     private void md5Init() {  
    85.         count[0] = 0L;  
    86.         count[1] = 0L;  
    87.         // /* Load magic initialization constants.  
    88.         state[0] = 0x67452301L;  
    89.         state[1] = 0xefcdab89L;  
    90.         state[2] = 0x98badcfeL;  
    91.         state[3] = 0x10325476L;  
    92.         return;  
    93.     }  
    94.   
    95.     /*  
    96.      * F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是  
    97.      * 简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们 实现成了private方法,名字保持了原来C中的。  
    98.      */  
    99.     private long F(long x, long y, long z) {  
    100.         return (x & y) | ((~x) & z);  
    101.     }  
    102.   
    103.     private long G(long x, long y, long z) {  
    104.         return (x & z) | (y & (~z));  
    105.     }  
    106.   
    107.     private long H(long x, long y, long z) {  
    108.         return x ^ y ^ z;  
    109.     }  
    110.   
    111.     private long I(long x, long y, long z) {  
    112.         return y ^ (x | (~z));  
    113.     }  
    114.   
    115.     /* 
    116.      * Rotation is separate from addition to prevent recomputation. 
    117.      */  
    118.     private long FF(long a, long b, long c, long d, long x, long s, long ac) {  
    119.         a += F(b, c, d) + x + ac;  
    120.         a = ((int) a << s) | ((int) a >>> (32 - s));  
    121.         a += b;  
    122.         return a;  
    123.     }  
    124.   
    125.     private long GG(long a, long b, long c, long d, long x, long s, long ac) {  
    126.         a += G(b, c, d) + x + ac;  
    127.         a = ((int) a << s) | ((int) a >>> (32 - s));  
    128.         a += b;  
    129.         return a;  
    130.     }  
    131.   
    132.     private long HH(long a, long b, long c, long d, long x, long s, long ac) {  
    133.         a += H(b, c, d) + x + ac;  
    134.         a = ((int) a << s) | ((int) a >>> (32 - s));  
    135.         a += b;  
    136.         return a;  
    137.     }  
    138.   
    139.     private long II(long a, long b, long c, long d, long x, long s, long ac) {  
    140.         a += I(b, c, d) + x + ac;  
    141.         a = ((int) a << s) | ((int) a >>> (32 - s));  
    142.         a += b;  
    143.         return a;  
    144.     }  
    145.   
    146.     /* 
    147.      * md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个 
    148.      * 函数由getMD5ofStr调用,调用之前需要调用md5init,因此把它设计成private的 
    149.      */  
    150.     private void md5Update(byte[] inbuf, int inputLen) {  
    151.         int i, index, partLen;  
    152.         byte[] block = new byte[64];  
    153.         index = (int) (count[0] >>> 3) & 0x3F;  
    154.         // /* Update number of bits */  
    155.         if ((count[0] += (inputLen << 3)) < (inputLen << 3))  
    156.             count[1]++;  
    157.         count[1] += (inputLen >>> 29);  
    158.         partLen = 64 - index;  
    159.         // Transform as many times as possible.  
    160.         if (inputLen >= partLen) {  
    161.             md5Memcpy(buffer, inbuf, index, 0, partLen);  
    162.             md5Transform(buffer);  
    163.             for (i = partLen; i + 63 < inputLen; i += 64) {  
    164.                 md5Memcpy(block, inbuf, 0, i, 64);  
    165.                 md5Transform(block);  
    166.             }  
    167.             index = 0;  
    168.         } else  
    169.             i = 0;  
    170.         // /* Buffer remaining input */  
    171.         md5Memcpy(buffer, inbuf, index, i, inputLen - i);  
    172.     }  
    173.   
    174.     /* 
    175.      * md5Final整理和填写输出结果 
    176.      */  
    177.     private void md5Final() {  
    178.         byte[] bits = new byte[8];  
    179.         int index, padLen;  
    180.         // /* Save number of bits */  
    181.         Encode(bits, count, 8);  
    182.         // /* Pad out to 56 mod 64.  
    183.         index = (int) (count[0] >>> 3) & 0x3f;  
    184.         padLen = (index < 56) ? (56 - index) : (120 - index);  
    185.         md5Update(PADDING, padLen);  
    186.         // /* Append length (before padding) */  
    187.         md5Update(bits, 8);  
    188.         // /* Store state in digest */  
    189.         Encode(digest, state, 16);  
    190.     }  
    191.   
    192.     /* 
    193.      * md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的 
    194.      * 字节拷贝到output的outpos位置开始 
    195.      */  
    196.     private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,  
    197.             int len) {  
    198.         int i;  
    199.         for (i = 0; i < len; i++)  
    200.             output[outpos + i] = input[inpos + i];  
    201.     }  
    202.   
    203.     /* 
    204.      * md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节 
    205.      */  
    206.     private void md5Transform(byte block[]) {  
    207.         long a = state[0], b = state[1], c = state[2], d = state[3];  
    208.         long[] x = new long[16];  
    209.         Decode(x, block, 64);  
    210.   
    211.         /* Round 1 */  
    212.         a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */  
    213.         d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */  
    214.         c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */  
    215.         b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */  
    216.         a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */  
    217.         d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */  
    218.         c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */  
    219.         b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */  
    220.         a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */  
    221.         d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */  
    222.         c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */  
    223.         b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */  
    224.         a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */  
    225.         d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */  
    226.         c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */  
    227.         b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */  
    228.         /* Round 2 */  
    229.         a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */  
    230.         d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */  
    231.         c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */  
    232.         b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */  
    233.         a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */  
    234.         d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */  
    235.         c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */  
    236.         b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */  
    237.         a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */  
    238.         d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */  
    239.         c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */  
    240.         b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */  
    241.         a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */  
    242.         d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */  
    243.         c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */  
    244.         b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */  
    245.         /* Round 3 */  
    246.         a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */  
    247.         d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */  
    248.         c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */  
    249.         b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */  
    250.         a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */  
    251.         d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */  
    252.         c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */  
    253.         b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */  
    254.         a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */  
    255.         d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */  
    256.         c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */  
    257.         b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */  
    258.         a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */  
    259.         d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */  
    260.         c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */  
    261.         b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */  
    262.         /* Round 4 */  
    263.         a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */  
    264.         d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */  
    265.         c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */  
    266.         b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */  
    267.         a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */  
    268.         d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */  
    269.         c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */  
    270.         b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */  
    271.         a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */  
    272.         d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */  
    273.         c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */  
    274.         b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */  
    275.         a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */  
    276.         d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */  
    277.         c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */  
    278.         b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */  
    279.         state[0] += a;  
    280.         state[1] += b;  
    281.         state[2] += c;  
    282.         state[3] += d;  
    283.     }  
    284.   
    285.     /* 
    286.      * Encode把long数组按顺序拆成byte数组,因为java的long类型是64bit的, 只拆低32bit,以适应原始C实现的用途 
    287.      */  
    288.     private void Encode(byte[] output, long[] input, int len) {  
    289.         int i, j;  
    290.         for (i = 0, j = 0; j < len; i++, j += 4) {  
    291.             output[j] = (byte) (input[i] & 0xffL);  
    292.             output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);  
    293.             output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);  
    294.             output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);  
    295.         }  
    296.     }  
    297.   
    298.     /* 
    299.      * Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的, 
    300.      * 只合成低32bit,高32bit清零,以适应原始C实现的用途 
    301.      */  
    302.     private void Decode(long[] output, byte[] input, int len) {  
    303.         int i, j;  
    304.         for (i = 0, j = 0; j < len; i++, j += 4)  
    305.             output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)  
    306.                     | (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);  
    307.         return;  
    308.     }  
    309.   
    310.     /** 
    311.      * b2iu是我写的一个把byte按照不考虑正负号的原则的"升位"程序,因为java没有unsigned运算 
    312.      */  
    313.     public static long b2iu(byte b) {  
    314.         return b < 0 ? b & 0x7F + 128 : b;  
    315.     }  
    316.   
    317.     /** 
    318.      * byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示, 
    319.      * 因为java中的byte的toString无法实现这一点,我们又没有C语言中的 sprintf(outbuf,"%02X",ib) 
    320.      */  
    321.     public static String byteHEX(byte ib) {  
    322.         char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',  
    323.                 'B', 'C', 'D', 'E', 'F' };  
    324.         char[] ob = new char[2];  
    325.         ob[0] = Digit[(ib >>> 4) & 0X0F];  
    326.         ob[1] = Digit[ib & 0X0F];  
    327.         String s = new String(ob);  
    328.         return s;  
    329.     }  
    330.       
    331.       
    332.     //-----------------------------------------------------------  
    333.   
    334.     /** 
    335.      * 获取单个文件的MD5值! 
    336.      * @param file 
    337.      * @return 
    338.      */  
    339.     public static String getFileMD5(File file) {  
    340.         if (!file.isFile()) {  
    341.             return null;  
    342.         }  
    343.         MessageDigest digest = null;  
    344.         FileInputStream in = null;  
    345.         byte buffer[] = new byte[1024];  
    346.         int len;  
    347.         try {  
    348.             digest = MessageDigest.getInstance("MD5");  
    349.             in = new FileInputStream(file);  
    350.             while ((len = in.read(buffer, 0, 1024)) != -1) {  
    351.                 digest.update(buffer, 0, len);  
    352.             }  
    353.             in.close();  
    354.         } catch (Exception e) {  
    355.             e.printStackTrace();  
    356.             return null;  
    357.         }  
    358.         BigInteger bigInt = new BigInteger(1, digest.digest());  
    359.         return bigInt.toString(16).toUpperCase();  
    360.     }  
    361.         
    362.     /** 
    363.      * 获取单个文件的SHA1值! 
    364.      *  
    365.      * @param file 
    366.      * @return 
    367.      */  
    368.     public static String getFileSHA1(File file) {  
    369.         if (!file.isFile()) {  
    370.             return null;  
    371.         }  
    372.         MessageDigest digest = null;  
    373.         FileInputStream in = null;  
    374.         byte buffer[] = new byte[1024];  
    375.         int len;  
    376.         try {  
    377.             digest = MessageDigest.getInstance("SHA1");  
    378.             in = new FileInputStream(file);  
    379.             while ((len = in.read(buffer, 0, 1024)) != -1) {  
    380.                 digest.update(buffer, 0, len);  
    381.             }  
    382.             in.close();  
    383.         } catch (Exception e) {  
    384.             e.printStackTrace();  
    385.             return null;  
    386.         }  
    387.         BigInteger bigInt = new BigInteger(1, digest.digest());  
    388.         return bigInt.toString(16).toUpperCase();  
    389.     }  
    390.       
    391.     /** 
    392.      * 获取单个文件的CRC32值! 
    393.      * @param file 
    394.      * @return 
    395.      * @throws Exception 
    396.      */  
    397.     public static String getFileCRC32(File file) {  
    398.         FileInputStream fileinputstream;  
    399.         try {  
    400.             fileinputstream = new FileInputStream(file);  
    401.             CRC32 crc32 = new CRC32();  
    402.             for (CheckedInputStream checkedinputstream = new CheckedInputStream(  
    403.                     fileinputstream, crc32); checkedinputstream.read() != -1;) {  
    404.             }  
    405.             return Long.toHexString(crc32.getValue());  
    406.         } catch (FileNotFoundException e) {  
    407.             e.printStackTrace();  
    408.         } catch (IOException e) {  
    409.             e.printStackTrace();  
    410.         }  
    411.         return null;  
    412.     }  
    413.   
    414.       
    415.     /** 
    416.      * 获取文件夹中文件的MD5值 
    417.      *  
    418.      * @param file 
    419.      * @param listChild 
    420.      *            ;true递归子目录中的文件 
    421.      * @return 
    422.      */  
    423.     public static Map<String, String> getDirMD5(File file, boolean listChild) {  
    424.         if (!file.isDirectory()) {  
    425.             return null;  
    426.         }  
    427.         // <filepath,md5>  
    428.         Map<String, String> map = new HashMap<String, String>();  
    429.         String md5;  
    430.         File files[] = file.listFiles();  
    431.         for (int i = 0; i < files.length; i++) {  
    432.             File f = files[i];  
    433.             if (f.isDirectory() && listChild) {  
    434.                 map.putAll(getDirMD5(f, listChild));  
    435.             } else {  
    436.                 md5 = getFileMD5(f);  
    437.                 if (md5 != null) {  
    438.                     map.put(f.getPath(), md5);  
    439.                 }  
    440.             }  
    441.         }  
    442.         return map;  
    443.     }  
    444.       
    445.     /** 
    446.      * 获取文件夹中文件的SHA1值 
    447.      *  
    448.      * @param file 
    449.      * @param listChild 
    450.      *            ;true递归子目录中的文件 
    451.      * @return 
    452.      */  
    453.     public static Map<String, String> getDirSHA1(File file, boolean listChild) {  
    454.         if (!file.isDirectory()) {  
    455.             return null;  
    456.         }  
    457.         // <filepath,SHA1>  
    458.         Map<String, String> map = new HashMap<String, String>();  
    459.         String sha1;  
    460.         File files[] = file.listFiles();  
    461.         for (int i = 0; i < files.length; i++) {  
    462.             File f = files[i];  
    463.             if (f.isDirectory() && listChild) {  
    464.                 map.putAll(getDirSHA1(f, listChild));  
    465.             } else {  
    466.                 sha1 = getFileSHA1(f);  
    467.                 if (sha1 != null) {  
    468.                     map.put(f.getPath(), sha1);  
    469.                 }  
    470.             }  
    471.         }  
    472.         return map;  
    473.     }  
    474.   
    475.     /** 
    476.      * 获取文件夹中文件的CRC32值 
    477.      *  
    478.      * @param file 
    479.      * @param listChild 
    480.      *            ;true递归子目录中的文件 
    481.      * @return 
    482.      */  
    483.     public static Map<String, String> getDirCRC32(File file, boolean listChild) {  
    484.         if (!file.isDirectory()) {  
    485.             return null;  
    486.         }  
    487.         // <filepath,CRC32>  
    488.         Map<String, String> map = new HashMap<String, String>();  
    489.         String crc32;  
    490.         File files[] = file.listFiles();  
    491.         for (int i = 0; i < files.length; i++) {  
    492.             File f = files[i];  
    493.             if (f.isDirectory() && listChild) {  
    494.                 map.putAll(getDirCRC32(f, listChild));  
    495.             } else {  
    496.                 crc32 = getFileCRC32(f);  
    497.                 if (crc32 != null) {  
    498.                     map.put(f.getPath(), crc32);  
    499.                 }  
    500.             }  
    501.         }  
    502.         return map;  
    503.     }  
    504. }  

    相关文章: