base64编码解码class文件
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;
/**
* @Author:novy
* @Date:12:05 2022/7/6
* @Version 1.0
**/
public class B64code {
public static void main(String[] args) throws Exception {
//解码还原成this.class
// String str = "base64字符串";
// decoderBase64File(str, "F:\\this.class");
//编码到this.txt
String base64Code = encodeBase64File("F:\\this.class");
//编码结果会有换行,所以要把换行删掉
String Str2 = base64Code.replaceAll("(\\r\\n|\\n|\\\\n|\\s)", "");
System.out.println(Str2.trim());
try (FileOutputStream fileOutputStream = new FileOutputStream("F:\\this.txt")) {
byte[] bytes = Str2.getBytes();
fileOutputStream.write(bytes);
}
}
public static String encodeBase64File(String path) throws Exception {
File file = new File(path);
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
inputFile.read(buffer);
inputFile.close();
return new BASE64Encoder().encode(buffer);
}
public static void decoderBase64File(String base64Code,String targetPath) throws Exception {
byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
}
}