• 计算MD5码


    应用环境:计算MD5码时上报进度

    主要代码:

    import java.security.MessageDigest;

    MessageDigest digest=MessageDigest.getInstance("md5");

    digest.update(buf,0,count);//将buf中0-count的字节放到digest中

    md5Bytes=digest.digest();//生成hashcode

    //计算md5码

    for (int i = 0; i < md5Bytes.length; i++) {
       int val = ((int) md5Bytes[i]) & 0xff;
       if (val < 16)
      hexValue.append("0");
       hexValue.append(Integer.toHexString(val));
     }

    View Code
     1  @Override
     2   public void doTask() throws Exception {
     3     MessageDigest digest = MessageDigest.getInstance("md5");
     4     File file = new File(path);
     5     // Task task = new Task();
     6     // setTask(task);
     7     // setTotalSize(file.length());
     8     if (!file.exists()) {
     9       // throw file not exist exception
    10       throw new FileNotFoundException("File don't exist!");
    11     }
    12     InputStream is = new BufferedInputStream(new FileInputStream(file));
    13     int count = 0;
    14     long totalCalculated = 0;
    15     byte[] buf = new byte[BUFFER];
    16     boolean cancelled = false;
    17     while ((count = is.read(buf, 0, BUFFER)) != -1) {
    18       // digest bytes
    19       digest.update(buf, 0, count);
    20       totalCalculated += count;
    21       // fire task progress
    22       taskProgressChanged(totalCalculated);
    23       if (!check()) {
    24         cancelled = true;
    25         break;
    26       }
    27     }
    28     is.close();
    29     byte[] md5Bytes;
    30     StringBuffer hexValue = new StringBuffer();
    31     md5Bytes = digest.digest();
    32     for (int i = 0; i < md5Bytes.length; i++) {
    33       int val = ((int) md5Bytes[i]) & 0xff;
    34       if (val < 16)
    35         hexValue.append("0");
    36       hexValue.append(Integer.toHexString(val));
    37     }
    38     // System.out.println(hexValue.toString());
    39     String md5 = hexValue.toString();
    40     if (cancelled) {
    41       // save md5 to database
    42       templateImageDao.saveMd5(templateImageId, md5);
    43     }
    44   }
    45 
    46   private String templateId;
    47   private int templateVersion;
    48   private String templateImageId;
    49   private String templateImageName;
    50   // absolute path
    51   private String path;
    52 
    53   private TemplateImageDao templateImageDao = new TemplateImageDao();
    54   public static final int BUFFER = 8192;
    55 
    56   // public static void main(String[] args) {
    57   // CalculateDigestSubTask c = new CalculateDigestSubTask();
    58   // c.setPath("e:\\test\\test1\\11.txt");
    59   // try {
    60   // c.doTask();
    61   // } catch (Exception e) {
    62   // // TODO Auto-generated catch block
    63   // e.printStackTrace();
    64   // }
    65   // }
  • 相关阅读:
    ASP.NET编程的十大技巧
    C#学习心得(转)
    POJ 1177 Picture (线段树)
    POJ 3067 Japan (树状数组)
    POJ 2828 Buy Tickets (线段树)
    POJ 1195 Mobile phones (二维树状数组)
    HDU 4235 Flowers (线段树)
    POJ 2886 Who Gets the Most Candies? (线段树)
    POJ 2418 Cows (树状数组)
    HDU 4339 Query (线段树)
  • 原文地址:https://www.cnblogs.com/sunflower627/p/2780897.html
Copyright © 2020-2023  润新知