import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64Util {
private final static String CHARSET = "UTF-8";
// 编码并写入文件
public static void encode(File file, String str) throws Exception {
String base64 = new BASE64Encoder().encode(str.getBytes(CHARSET));
Files.write(Paths.get(file.getAbsolutePath()), base64.getBytes(), StandardOpenOption.CREATE);
}
// 读文件并解码
public static String decode(File file) throws Exception {
byte[] in = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
byte[] b = new BASE64Decoder().decodeBuffer(new String(in));
return new String(b, CHARSET);
}
}