public static void imgChangeBase64(String fromPath, String toPath) {
try {
Base64.Encoder en = Base64.getEncoder();
FileInputStream fi = new FileInputStream(fromPath);
FileWriter fw = new FileWriter(toPath);
int available = fi.available();
byte[] bytes = new byte[available];
fi.read(bytes);
String base64str = en.encodeToString(bytes);
fw.write(base64str);
fw.flush();
fw.close();
fi.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
填入文件路径,调用即可。
toPath路径文件内得到的编码,
<img src="data:image/jpg;base64,编码粘贴此处(什么格式都不加)" />
<img src="data:image/png;base64,编码粘贴此处(什么格式都不加)" />
即可显示。