public static String base64encode(String text) {
   try {
     String rez = enc.encode(text.getBytes(DEFAULT_ENCODING));
     return rez;
   } catch (UnsupportedEncodingException e) {
     return null;
   }
 } // base64encode
示例#2
0
  /**
   * 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
   *
   * @param imgSrcPath 生成64编码的图片的路径
   * @return
   */
  public static String getImageStr(String imgSrcPath) {
    InputStream in = null;
    byte[] data = null;

    // 读取图片字节数组
    try {
      in = new FileInputStream(imgSrcPath);
      data = new byte[in.available()];
      in.read(data);
      in.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    // 对字节数组Base64编码
    BASE64Encoder encoder = new BASE64Encoder();

    return encoder.encode(data); // 返回Base64编码过的字节数组字符串
  }