java 字符串转Base64

场景一:

String base64encodedString = Base64.getEncoder().encodeToString("123".getBytes("utf-8"));
	         System.out.println("Base64 编码字符串 (基本) :" + base64encodedString);

场景二:

String str = "123";
byte[] datas = str.getBytes();
bs.write(datas,0,datas.length);
bs.flush();
byte[] dest = bs.toByteArray();
String s0 =Base64.getEncoder().encodeToString((dest)); 

场景三:

ByteArrayOutputStream bos = null;
FileInputStream fs = null;
 try
 {
	 bos = new ByteArrayOutputStream();
	  fs = new FileInputStream("D:\\1.txt");
	  int len;
	  while((len = fs.read()) != -1)
	  {
	           bos.write(len);
	   }
	   //转换成byte字符数组
	   byte[] array = bos.toByteArray();
	   String s1 =Base64.getEncoder().encodeToString((bos.toByteArray()));
	   // 指定解码的字符集utf-8
	    System.out.println(new String(array, "utf-8"));
	     fs.close();
}
catch(IOException e)
{}
finally
{
	bos.close();
	 fs.close();
}

  

  

posted @ 2021-04-01 10:45  zhaogaojian  阅读(10411)  评论(0编辑  收藏  举报