BASE64操作

    xiaoxiao2022-07-14  162

    加密 /** * base64加密 * @param s * @return */ @SuppressWarnings("restriction") public static String getBASE64(String s) { if (s == null) return null; return (new sun.misc.BASE64Encoder()).encode( s.getBytes() ); } 解密 /** * 解密 * @param s * @return */ @SuppressWarnings("restriction") public static String getFromBASE64(String s) { if (s == null) return null; sun.misc.BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b); } catch (IOException e) { return null; } } 转为图片 /** * base64转为图片 * @param str * @param imgFilePath * @return */ @SuppressWarnings("restriction") public static OutputStream GenerateImage(String str, String imgFilePath) { if (str == null) return null;// 图像数据为空 sun.misc.BASE64Decoder decoder = new BASE64Decoder(); try { // Base64解码 byte[] bytes = decoder.decodeBuffer(str); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) {// 调整异常数据 bytes[i] += 256; } } // 生成jpeg图片 OutputStream out = new FileOutputStream(imgFilePath); out.write(bytes); out.flush(); out.close(); return out; } catch (Exception e) { return null; } } 页面接收图片 <img id="images"> $("#images").attr("src","data:image/gif;base64,"+data.photo); //放入ajax success函数

     

    最新回复(0)