📄 base64code.java
字号:
package com.md5.chen;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import java.io.*;
import java.security.DigestInputStream;
import java.security.MessageDigest;
//获取BASE64编码的文件MD5
public class Base64Code
{
private Base64Code()
{
}
public static byte[] encode(byte[] buf)
{
if (buf == null)
{
return encode(buf, 0, 0);
} else
{
return encode(buf, 0, buf.length);
}
}
// 将 buf 进行 BASE64 编码
public static byte[] encode(byte[] buf, int pos, int length)
{
byte[] result = null;
if (length > 0)
{
if (pos == 0 && buf.length == length)
{
return Base64.encodeBase64(buf);
} else
{
byte[] newbuf = new byte[length];
System.arraycopy(buf, pos, newbuf, 0, length);
result = Base64.encodeBase64(newbuf);
}
}
return result;
}
// 将 BASE64 编码的字符串 buf 进行解码
public static byte[] decode(byte[] buf, int pos, int length)
throws IOException
{
byte[] result = null;
if (length > 0)
{
result = FastBase64.decodeBase64(buf, pos, length);
// if (pos == 0 && buf.length == length) {
// result = Base64.decodeBase64(buf);
// } else {
// byte[] newbuf = new byte[length];
// System.arraycopy(buf, pos, newbuf, 0, length);
// result = Base64.decodeBase64(newbuf);
// }
}
return result;
}
public static void main(String[] args)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
String PATH="";
String md5String="";
byte k[]=null;
System.out.println("输入文件路径:");
try
{
PATH = reader.readLine();// 将主目录路径保存再PATH中
} catch (IOException e)
{
e.toString();
}
try
{ // 生成MessageDigest对象MD
MessageDigest md = MessageDigest.getInstance("MD5");
FileInputStream fin = new FileInputStream(PATH);
DigestInputStream din = new DigestInputStream(fin, md);
while (din.read() != -1)
{
;
}
// 计算MD5 并进行BASE64编码
md5String = new String(Base64Code.encode(md.digest()));
} catch (Exception e)
{
e.toString();
}
System.out.println("BASE64-MD5:"+md5String);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -