在android中获取文件的MD5值

	public String getMD5(T file){

		int len;
		MessageDigest digest = null;
		InputStream is = null;
		byte buffer[] = new byte[1024];
		try {
			digest = MessageDigest.getInstance("MD5");
			if(file.getClass().equals(jcifs.smb.SmbFile.class))
			{
				is = new BufferedInputStream(new SmbFileInputStream((SmbFile)file));
			}
			else if(file.getClass().equals(java.io.File.class))
			{
				is = new BufferedInputStream(new FileInputStream((File)file));
			}
			else
				return null;
			while (-1!=(len = is.read(buffer,0,1024))) {
				
				digest.update(buffer,0,len);
			}
			
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		BigInteger bigInteger = new BigInteger(1, digest.digest());
		return bigInteger.toString(16);	
	}

你可能感兴趣的:(JAVA,Android)