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("\n"); 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); }
Looking for a job working at Home about MSBI