扩展FileUtil

This commit is contained in:
yexuejc-vm-win10 2022-05-08 19:30:08 +08:00
parent b97fdf65bc
commit 8741562463

View File

@ -285,7 +285,17 @@ public class FileUtil {
* @param file * @param file
* @return * @return
*/ */
public static String base64(File file) { public static String base64ToStr(File file) {
return new String(base64(file));
}
/**
* 获取文件base64
*
* @param file
* @return
*/
public static byte[] base64(File file) {
FileInputStream fileInputStream = null; FileInputStream fileInputStream = null;
byte[] data = null; byte[] data = null;
// 读取图片字节数组 // 读取图片字节数组
@ -299,12 +309,22 @@ public class FileUtil {
e.printStackTrace(); e.printStackTrace();
} }
// 对字节数组Base64编码 // 对字节数组Base64编码
return new String(Base64.getEncoder().encode(data)); return Base64.getEncoder().encode(data);
} }
public static void main(String[] args) { /**
String s = base64(new File("Z:\\NAS\\04.coding\\yexuejc\\yexuejc-base")); * base64转文件
System.out.printf( s); * <p>
* <i>
* 文件转base64请使用 {@link FileUtil#base64(File)}
* </i>
*
* @param decode {@link FileUtil#base64ToStr(File)} 的结果
* @param fileName 文件名称包含路径
* @return 返回保存地址
*/
public static String base64ToFile(String decode, String fileName) {
return base64ToFile(Base64.getDecoder().decode(decode.getBytes()), fileName);
} }
/** /**