• 获取文件的MD5值


    public  void getMd5OfFile() {
          //这是从mongodb获取的文件流 //DBObject dbObject
    = new BasicDBObject("filename","yansebuyiyangdeyanhuo5"); //DB db = mongoTemplate.getDb(); //GridFS fs = new GridFS(db,"myfiles"); //GridFSDBFile gridFSDBFile = fs.findOne(dbObject); //InputStream in = gridFSDBFile.getInputStream();
         String filePath = "文件路径";
         InputStream is = null;
         try{   in = new FileInputStram(new File(filePath));
    MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte[] bytes = new byte[1024]; int byteCount = 0; while ((byteCount = in.read(bytes)) !=-1) { messageDigest.update(bytes, 0, byteCount); } byte[] digest = messageDigest.digest(); // byte -128 ---- 127 StringBuffer sb = new StringBuffer(); for (byte b : digest) { int a = b & 0xff; // Log.d(TAG, "" + a); String hex = Integer.toHexString(a); if (hex.length() == 1) { hex = 0 + hex; } sb.append(hex); } System.err.println("MD5值为:================================"+sb.toString()); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } in = null; } } }
    //获取字符串的md5值
    public void getMd5ofString( ){ String text = "AABJRU5ErkJggg=="; try { MessageDigest messageDigest = MessageDigest.getInstance("md5"); byte[] buffer = messageDigest.digest(text.getBytes()); // byte -128 ---- 127 StringBuffer sb = new StringBuffer(); for (byte b : buffer) { int a = b & 0xff; String hex = Integer.toHexString(a); if (hex.length() == 1) { hex = 0 + hex; } sb.append(hex); } System.err.println("MD5值为:================================"+sb.toString()); } catch (NoSuchAlgorithmException e) { } }
    人生没有彩排,每天都是现场直播!
  • 相关阅读:
    MongoDB集群与LBS应用系列(一)
    Docker下构建centos7容器无法使用systemctl命令的解决办法
    重新诠释的OSGi规范
    交互细节中那些变形金刚
    UX黑名单!有哪些常见的移动端UX设计误区需要规避?
    那些门户网站的兼容问题(持续更新)
    Vmware虚拟机网络模式及虚拟机与物理机通信方法
    交互设计阅读(一)
    笑傲江湖:完美解决205:您所选择的线路不存在问题
    笑傲江湖 外网
  • 原文地址:https://www.cnblogs.com/northern-light/p/8908429.html
Copyright © 2020-2023  润新知