贴几个最近用到的工具类

1,获取如片的MD5和HASH值

package com.Tools;

import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

import com.sqlite.Dao.NcpConsumeDAO;
import com.sqlite.Model.Ncp_Consume;

public class MD5_SHA_Util {
    private final static String MD51 = "MD5";
    // 产生哈希值
    String hashType = "MD5";
    public static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    // 直接给出MD5的16位二进制码
    // public static byte[] getMD5byte(String fileName, String hashType)throws
    // Exception {
    public byte[] getMD5byte(String fileName) throws Exception {

        InputStream fis;
        fis = new FileInputStream(fileName);
        byte[] buffer = new byte[1024];
        MessageDigest md5 = MessageDigest.getInstance("MD5");

        int numRead = 0;
        while ((numRead = fis.read(buffer)) > 0) {
            md5.update(buffer, 0, numRead);
        }
        fis.close();
        byte b[] = md5.digest();
        System.out.println("byte b[]" + b);
        return b;

    }

    // 直接给出hash的16位二进制码
    // public static byte[] getHASHByte(String fileName, String hashType)throws
    // Exception {
    public byte[] getHASHByte(String fileName) throws Exception {
        InputStream fis;
        fis = new FileInputStream(fileName);
        byte[] buffer = new byte[1024];
        MessageDigest md5 = MessageDigest.getInstance("SHA1");

        int numRead = 0;
        while ((numRead = fis.read(buffer)) > 0) {
            md5.update(buffer, 0, numRead);
        }
        fis.close();
        byte b[] = md5.digest();
        return b;
    }

    public static String getHash(String fileName, String hashType)
            throws Exception {
        InputStream fis;
        fis = new FileInputStream(fileName);
        byte[] buffer = new byte[1024];
        MessageDigest md5 = MessageDigest.getInstance(hashType);

        int numRead = 0;
        while ((numRead = fis.read(buffer)) > 0) {
            md5.update(buffer, 0, numRead);
        }
        fis.close();
        byte b[] = md5.digest();
        return toHexString(md5.digest());
    }

    public static String getByteHash(byte[] bytearray, String hashType)
            throws Exception {
        byte[] fis = bytearray;
        byte[] buffer = new byte[1024];
        MessageDigest md5 = MessageDigest.getInstance(hashType);
        int numRead = 0;
        while ((numRead = fis.length) > 0) {
            md5.update(buffer, 0, numRead);
        }

        return toHexString(md5.digest());
    }

    public static String toHexString(byte[] b) {
        StringBuilder sb = new StringBuilder(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
            sb.append(hexChar[b[i] & 0x0f]);
        }
        return sb.toString();
    }

2,获取图片的二进制码

package com.Tools;

//输入流  

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.R.integer;

public class FingerImage_Convert {

    public int len;
    public integer date_len;
    @SuppressWarnings("finally")
    public byte[] getRimg(String From_Url) throws IOException {

        new File(From_Url).createNewFile();
        FileInputStream in = new FileInputStream(From_Url);
        byte[] bytes;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {

            byte[] b = new byte[2048];
            while ((len = in.read(b, 0, 2048)) != -1) {
                baos.write(b, 0, len);
            }

            baos.flush();

            // new File(From_Url).createNewFile();
            // FileInputStream in = new FileInputStream(From_Url);
            //
            // BufferedInputStream bis = new BufferedInputStream(in);
            //
            // while((i=in.read())!=-1){
            //
            // builder.append((char)i);
            //
            // }
            //
            // bis.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            bytes = baos.toByteArray();
            
            System.out.print("return bytes>>>>>>>>>>>>" + bytes);
            return bytes;

        }

    }

    // 输出流
    public int getLen(String From_Url) {
        return len;
    }

    public void setLen(int len) {
        this.len = len;
    }

    public void getWimg(String content, String to_Url) {

        try {

            new File(to_Url).createNewFile();

            FileOutputStream out = new FileOutputStream(to_Url);

            BufferedOutputStream bos = new BufferedOutputStream(out);

            char[] ch = content.toCharArray();

            // System.out.println(ch);

            for (int i = 0; i < ch.length; i++) {

                bos.write(ch[i]);

            }

            bos.flush();

            bos.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

3,获取网络时间并转化成年月日时分秒的纯数字字符串例如:20121212121212(2012年12月12日12时12分12秒)

import java.text.SimpleDateFormat;

import java.util.Date;

public class JavaTime {

    public String GetTime() {
        SimpleDateFormat formatter = new SimpleDateFormat(
                "yyyy年MM月dd日 HH:mm:ss ");
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        String str = formatter.format(curDate);
        String year = str.substring(2, 4);
        String month = str.substring(5, 7);
        String data = str.substring(8, 10);
        String Hour = str.substring(12, 14);
        String minute = str.substring(15, 17);
        String Second = str.substring(18, 20);
        String numsString = year + month + data + Hour + minute + Second;
        return numsString;

    }

}

4,SD卡操作的相关类

package com.Tools;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Environment;

public class FileUtils {
    private String SDPATH;

    public String getSDPATH() {
        return SDPATH;
    }
    public FileUtils() {
        //得到当前外部存储设备的目录
        // /SDCARD
        SDPATH = Environment.getExternalStorageDirectory() + "/"+"download";
    }
    /**
     * 在SD卡上创建文件
     * 
     * @throws IOException
     */
    public File creatSDFile(String fileName) throws IOException {
        File file = new File(SDPATH + "version");
        file.createNewFile();
        return file;
    }
    
    /**
     * 在SD卡上创建目录
     * 
     * @param dirName
     */
    public File creatSDDir(String dirName) {
        File dir = new File(SDPATH + dirName);
        dir.mkdirs();
        return dir;
    }

    /**
     * 判断SD卡上的文件夹是否存在
     */
    public boolean isFileExist(String fileName){
        File file = new File(SDPATH + fileName);
        return file.exists();
    }
    
    /**
     * 将一个InputStream里面的数据写入到SD卡中
     */
    public File write2SDFromInput(String path,String fileName,InputStream input){
        File file = null;
        OutputStream output = null;
        try{
            creatSDDir(path);
            file = creatSDFile(path + fileName);
            output = new FileOutputStream(file);
            byte buffer [] = new byte[4 * 1024];
            while((input.read(buffer)) != -1){
                output.write(buffer);
            }
            output.flush();
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            try{
                output.close();
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
        return file;
    }

}

 

 

 

 

posted @ 2012-06-15 13:45  神仙都是我自己  阅读(207)  评论(0编辑  收藏  举报