import sun.misc.BASE64Encoder; import java.io.*; public class TestBase64 { public static void main(String[] args) { String imgFile="C:\\1.png"; GetBase64Str(imgFile); } public static void GetBase64Str(String imgFile) { InputStream in = null; byte[] data = null; try { in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } BASE64Encoder encoder = new BASE64Encoder(); String reuslt = "<img src=\"data:image/png;base64,"+encoder.encode(data)+"\" alt=\"\" style=\"border: none; vertical-align: middle; max-width: 830px;\" />"; System.out.println(reuslt); File file =new File("C:\\print.txt"); FileOutputStream fos = null; try { fos = new FileOutputStream(file); fos.write(reuslt.getBytes()); } catch (Exception e) { e.printStackTrace(); } } }
JAVA图片转换Base64
最新推荐文章于 2024-08-23 14:24:12 发布