Java生成BASE64编码

时间:2023-03-09 12:57:17
Java生成BASE64编码

1.所需jar包:

BASE64Encoder.jar

2.在导包过程中,可能会出现工程不识别的情况。即:不能找到BASE64Encoder的jar包。此时,可以有两种方法解决这个问题:

a.  只需要在project build path 中先移除JRE System Liberary, 再添加库JRE System Liberary ,重新编译后就一切正常了。

b. Windows--->Preferences--->Java--->Compiler--->Errors/Warnings--->Deprecated and trstricted API --->Forbidden reference (access rules):--->change        to warning

ps:我是用方法a解决的这个问题。

3.详细代码实现如下:

 package com.itRed.base64Util;

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; import sun.misc.BASE64Encoder; public class Base64Util { /**
* 生成Base64编码
* @param <BASE64Encoder>
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub BASE64Encoder encoder=new BASE64Encoder();
System.out.println("please input username:");
String username=new BufferedReader(
new InputStreamReader(System.in)).readLine();
System.out.println(encoder.encode(username.getBytes()));
System.out.println("please input your password:");
String password=new BufferedReader(
new InputStreamReader(System.in)).readLine();
System.out.println(encoder.encode(password.getBytes()));
} }