sun.misc.BASE64Decoder的替换方案,使用java.util.Base64代替 淡淡的烟草味﹌ 2022-12-10 15:58 961阅读 0赞 ### 背景 ### 在项目进行编译时,控制台会出现以下警告信息,提示sun.misc.BASE64Decoder是内部API,会在将来的版本的进行删除,需要避免使用 ### 解决 ### 使用java.util.Base64来替代,如下: * 使用原始的BASE64Encoder public static String imageToBase64ByLocal(File file) { byte[] data = null; // 读取图片字节数组 try (InputStream in = new FileInputStream(file.getPath())) { data = new byte[in.available()]; in.read(data); } catch (IOException e) { e.printStackTrace(); } // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); // 返回Base64编码过的字节数组字符串 return encoder.encode(data); } * 更新为util包下的Base64.Encoder public static String imageToBase64ByLocal(File file) { byte[] data = null; // 读取图片字节数组 try (InputStream in = new FileInputStream(file.getPath())) { data = new byte[in.available()]; in.read(data); } catch (IOException e) { e.printStackTrace(); } // 对字节数组Base64编码 Base64.Encoder encoder = Base64.getEncoder(); // 返回Base64编码过的字节数组字符串 return encoder.encodeToString(data); } 同理,Decoder也一样,详情可以参考java8的api文档,里面还提供了好多的编码和解码的方法 ### 备注 ### sun.misc这个包是在rt.jar包下的 ![在这里插入图片描述][20200927120503116.png_pic_center] 只是apidoc中没有,那是因为sun开头的包不属于J2SE规范,是Sun公司的内部实现。 [20200927120503116.png_pic_center]: /images/20221123/b3d0ae304bfb4cafa94c42519f1a8f8f.png
还没有评论,来说两句吧...