package cdm;
import javaioFile;
import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioIOException;
import javamathBigInteger;
import javanioMappedByteBuffer;
import javaniochannelsFileChannel;
import javasecurityMessageDigest;
import orgapachecommonscodecdigest;
import orgapachecommonsioIOUtils;
public class testMD5 {
public static String getMd5ByFile(File file) throws FileNotFoundException {
String value = null;
FileInputStream in = new FileInputStream(file);
try {
MappedByteBuffer byteBuffer = ingetChannel()map(FileChannelMapModeREAD_ONLY, 0, filelength());
MessageDigest md5 = MessageDigestgetInstance("MD5");
md5update(byteBuffer);
BigInteger bi = new BigInteger(1, md5digest());
value = bitoString(16);
} catch (Exception e) {
eprintStackTrace();
} finally {
if(null != in) {
try {
inclose();
} catch (IOException e) {
eprintStackTrace();
}
}
}
return value;
}
public static void main(String[] args) throws IOException {
String path="E:\文件zip";
String v = getMd5ByFile(new File(path));
Systemoutprintln("MD5:"+vtoUpperCase());
FileInputStream fis= new FileInputStream(path);
String md5 = DigestUtilsmd5Hex(IOUtilstoByteArray(fis));
IOUtilscloseQuietly(fis);
Systemoutprintln("MD5:"+md5);
//Systemoutprintln("MD5:"+DigestUtilsmd5Hex("WANGQIUYUN"));
}
}
public static String getFileMD5(File file) {
if (!fileisFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigestgetInstance("MD5");
in = new FileInputStream(file);
while ((len = inread(buffer, 0, 1024)) != -1) {
digestupdate(buffer, 0, len);
}
inclose();
} catch (Exception e) {
eprintStackTrace();
return null;
}
BigInteger bigInt = new BigInteger(1, digestdigest());
return bigInttoString(16);
}
package mybag;
import javaioFile;
import javaioFileInputStream;
import javamathBigInteger;
import javasecurityMessageDigest;
import javautilHashMap;
import javautilMap;
import androidappActivity;
import androidosBundle;
import androidutilLog;
public class FileDigest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
superonCreate(savedInstanceState);
File file = new File("/mnt/sdcard/123txt");
Loge("MD5", getFileMD5(file));
}
public static String getFileMD5(File file) {
if (!fileisFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigestgetInstance("MD5");
in = new FileInputStream(file);
while ((len = inread(buffer, 0, 1024)) != -1) {
digestupdate(buffer, 0, len);
}
inclose();
} catch (Exception e) {
eprintStackTrace();
return null;
}
BigInteger bigInt = new BigInteger(1, digestdigest());
return bigInttoString(16);
}
public static Map getDirMD5(File file, boolean listChild) {
if (!fileisDirectory()) {
return null;
}
Map map = new HashMap();
String md5;
File files[] = filelistFiles();
for (int i = 0; i < fileslength; i++) {
File f = files[i];
if (fisDirectory() && listChild) {
mapputAll(getDirMD5(f, listChild));
} else {
md5 = getFileMD5(f);
if (md5 != null) {
mapput(fgetPath(), md5);
}
}
}
return map;
}
}
最后不要忘了在manifest中添加权限
以上就是关于Java,如何获取文件的MD5值全部的内容,包括:Java,如何获取文件的MD5值、安卓获取文件MD5值、android开发中怎么获取文件的正确md5值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)