android中获取文件的md5值时首位出现0被省略,而造成了只有31位字符的解决办法。

使用BigInteger 会出现首位为0时MD5值出现31位的现象。

//获取单个文件MD5  其中一种操作方式
    public static String getFileMD5(File file)   
    {  
        // TODO Auto-generated method stub  
        if(!file.isFile())  
        {  
            return null;  
        }  
        MessageDigest digest=null;  
        FileInputStream in=null;  
        byte buffer[]=new byte[1024];  
        int len;  
        try  
        {  
            digest=MessageDigest.getInstance("MD5");  
            in=new FileInputStream(file);  
            while((len=in.read(buffer, 0, 1024))!=-1)  
            {  
                digest.update(buffer, 0, len);  
            }  
            in.close();  
        }  
        catch(Exception e)  
        {  
            e.printStackTrace();  
            return null;  
        }  
          
        BigInteger bigint=new BigInteger(1,digest.digest());  
        return bigint.toString(16);  
    }  

//获取单个文件MD5  另一种操作方式
public String getMd5(File file){
        String value = null;
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(byteBuffer);
            BigInteger bi = new BigInteger(1, md5.digest());
            value = bi.toString(16);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return value;
    }


上面的两种方法都是使用了BigInteger 来保存后,再转化成字符串,都会出现首位为0被忽略的现象。

下面的方法可以解决上述的问题

public static String getFileMD5(File file) {

    StringBuffer stringbuffer=null;
    try {
        char hexDigits[] = { '0', '1', '2','3', '4','5', '6','7','8', '9', 'a','b' ,'c', 'd','e', 'f' };
        FileInputStream in = new FileInputStream(file);
        FileChannel ch = in.getChannel();
        MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,file.length());
        MessageDigest messagedigest = MessageDigest.getInstance("MD5");
        messagedigest.update(byteBuffer);
        byte[] bytes=messagedigest.digest();
        int n=bytes.length;
        stringbuffer = new StringBuffer(2 * n);
        for (int l = 0; l < n; l++) {
            byte bt=bytes[l];
            char c0 = hexDigits[(bt & 0xf0) >> 4];
            char c1 = hexDigits[bt & 0xf];
            stringbuffer.append(c0);
            stringbuffer.append(c1);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return stringbuffer.toString();

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值