android中获取文件的md5值时首位出现0被省略,而造成了只有31位字符的解决办法。
2016-05-10 22:17
573 查看
使用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被忽略的现象。
下面的方法可以解决上述的问题
//获取单个文件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(); } }
相关文章推荐
- Android基础知识:广播
- Android开发工具之Android Studio----版本控制SVN使用(一)
- android.support.v7.widget.Toolbar 中menu图标不显示问题
- Android View系列一: View基础知识
- Android中的通知 Notification
- Android的Handler,Looper消息机制问题
- android studio 学习笔记之 全面进阶配置技巧
- 【Android】自定义View —— 滑动的次数选择器
- ExpandableListView二级分栏详解完美用法
- Android View的事件分发
- Android——tabHost的使用注意点
- android:screenOrientation属性详解
- Exception raised during rendering: Could not initialize class android.widget.TextView Exception deta
- Android自定义Toast带图片的
- 马上搞定Android平台的Wi-Fi Direct开发
- AndroidManifest.xml详解
- android 6.0 textview 跑马灯失效和出现抖动的解决方法
- Android中级篇之Fresco-加载图片基础[详细图解Fresco的使用]
- Android 6.0开始动态请求权限
- Android之根据Uri获得图片或视频文件路径(解决4.4以上版本得不到路径的情况)