Java 图片、文件 Base64 互转
package com.thoth.his.base.util; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Base64; public class ImageUtil { public static String FileToBase64(String filePath) throws IOException { FileInputStream inputStream = null; try { Base64.Encoder encoder = Base64.getEncoder(); inputStream = new FileInputStream(filePath); int available = inputStream.available(); byte[] bytes = new byte[available]; inputStream.read(bytes); return encoder.encodeToString(bytes); } catch (Exception e) { e.printStackTrace(); } finally { inputStream.close(); } return ""; } public static boolean Base64ToFile(String base64Str, String saveFilePath) throws IOException { FileOutputStream outputStream = null; try { Base64.Decoder decoder = Base64.getDecoder(); byte[] bytes = decoder.decode(base64Str); outputStream = new FileOutputStream(saveFilePath); outputStream.write(bytes); return true; } catch (Exception e) { e.printStackTrace(); } finally { outputStream.close(); } return false; } }