Java get file MD5 check code

Source: Internet
Author: User

Thunder Download the core idea is to verify the MD5 value of the file, two files if the MD5 is the same file.

When a user requests to download a file, it MD5 the URL that owns the file based on the file retained in the database.

Hooks a user request to this URL and mimics a false breakpoint continuation request, stitching a normal file from several URLs.

Check the information, Java can also implement the file MD5 check code acquisition, the code is as follows:

import Java.io.File;
import Java.io.FileInputStream;
import java.io.IOException;
import Java.nio.MappedByteBuffer;
import Java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5util {

protected static char hexdigits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '};
protected static messagedigest messagedigest = null;
static{
   try{
    messagedigest = Messagedigest.getinstance ("MD5 ");
   }catch (NOSUCHALGORITHMEXCEPTION NSAEX) {
Span style= "FONT-SIZE:18PX;" >    system.err.println (MD5Util.class.getName () + "initialization failed, MessageDigest does not support Md5util. ");
    nsaex.printstacktrace ();
   }


public static String getfilemd5string (file file) throws IOException {
FileInputStream in = new FileInputStream (file);
FileChannel ch = in.getchannel ();
Mappedbytebuffer Bytebuffer = Ch.map (FileChannel.MapMode.READ_ONLY, 0, File.length ());
Messagedigest.update (Bytebuffer);
Return Buffertohex (Messagedigest.digest ());
}

public static string getmd5string (string s) {
Return getmd5string (S.getbytes ());
}

public static String getmd5string (byte[] bytes) {
Messagedigest.update (bytes);
Return Buffertohex (Messagedigest.digest ());
}

private static String Buffertohex (byte bytes[]) {
Return Buffertohex (bytes, 0, bytes.length);
}

private static String Buffertohex (byte bytes[], int m, int n) {
   stringbuffer StringBuffer = new StringBuffer (2 * n);
   int k = m + N;
   for (int l = m; l < K; l++) {
    appendhexpair (Bytes[l], stringbuffer);
&NBSP;&NBSP;&NBSP;}
   return stringbuffer.tostring ();


private static void Appendhexpair (Byte bt, StringBuffer StringBuffer) {
char C0 = hexdigits[(BT & 0XF0) >> 4];
Char C1 = HEXDIGITS[BT & 0xf];
Stringbuffer.append (C0);
Stringbuffer.append (C1);
}

public static Boolean Checkpassword (string password, string md5pwdstr) {
String s = getmd5string (password);
Return S.equals (MD5PWDSTR);
}

public static void Main (string[] args) throws IOException {
Long begin = System.currenttimemillis ();

2ea3e66ac37df7610f5bd322ec4ffe48 670M 11s Kuri dual-core 1.66G 2G memory
File Big = new file ("e:/new folder. rar");

String md5=getfilemd5string (BIG);

Long end = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("MD5:" +md5+ "Time:" + ((end-begin)/1000) + "s");
}

}

Reprinted from: http://blog.sina.com.cn/s/blog_53d3c24a0100mpm1.html

Java get file MD5 check code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.