zoukankan      html  css  js  c++  java
  • java md5

    需求是,上传文件到HDFS,然后生成同名的MD5文件,基本示例如下:

    public static String getMD5(InputStream inputStream)
        {
    	    byte[] buffer = new byte[1024];
            int len = 0;
            try
            {
                MessageDigest messageDigest = MessageDigest.getInstance("MD5");
                 while ((len = inputStream.read(buffer)) != -1)
                {	
                    messageDigest.update(buffer, 0, len);
                } 
                BigInteger bigInteger = new BigInteger(1, messageDigest.digest());
                String hashtext = bigInteger.toString(16);
                while(hashtext.length() < 32 ){
                	  hashtext = "0"+hashtext;
                	}
                return hashtext;
            }
            catch (Exception e)
            {
                return null;
            }
        }
    
       

    尤其需要注意的是第一段代码中的补0,如果不补0,这就会跟使用md5sum命令生成的数据有差别,这样使用md5sum -c 进行检查的时候,是会报错的。

    报数据的格式不正确,提示错误不是校验失败。

    	try {
    				//mkdir -p then upload file 
    				String resDir = new File(softwareEntity.getResUri()).getParentFile().getPath();
    				//System.out.println(resDir);
    				HdfsUtil.getInstance().mkdir(resDir);
    				HdfsUtil.getInstance().createFile(softwareEntity.getResUri(), file.getBytes());
    				//upload the md5 file to the same directory
     	        String md5=EncryptionUtil.getMD5(file.getInputStream());
     	        StringBuilder sbFileCont = new StringBuilder(md5);
     			if(md5!=null)
     			{
     				String twoSpaces = "  " ;
     				sbFileCont.append(twoSpaces);
     				sbFileCont.append(file.getOriginalFilename());
     				sbFileCont.append("
    ");
     				HdfsUtil.getInstance().createFile(softwareEntity.getResUri()+".md5",sbFileCont.toString().getBytes());
     			}
     			else
     				throw new ServiceException("Something wrong when gererate md5 digest for file" + softwareEntity.getResUri());
    				
    			} catch (IOException e) {
    				throw new ServiceException("Save file error.", e);
    			} catch (URISyntaxException e) { 
    				throw new ServiceException("upload file to hdfs error",e);
    			}
    
  • 相关阅读:
    Spring常用注解
    mybatis注解映射的简单分类
    Java框架中各层作用简述
    maven中groupId和artifactId的含义
    mybatis缓存
    防盗链的基本原理
    将部分字符串转化成JsonArray
    风螺旋线的进入
    3D转弯保护区长啥样?
    风螺旋线公切线的算法
  • 原文地址:https://www.cnblogs.com/huaxiaoyao/p/5789650.html
Copyright © 2011-2022 走看看