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.详细代码实现如下:

 1 package com.itRed.base64Util;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 
 7 import sun.misc.BASE64Encoder;
 8 
 9 public class Base64Util {
10 
11 /**
12 * 生成Base64编码
13 * @param <BASE64Encoder>
14 * @param args
15 */
16 public static void main(String[] args) throws IOException{
17 // TODO Auto-generated method stub
18 
19 BASE64Encoder encoder=new BASE64Encoder();
20 System.out.println("please input username:");
21 String username=new BufferedReader(
22 new InputStreamReader(System.in)).readLine();
23 System.out.println(encoder.encode(username.getBytes()));
24 System.out.println("please input your password:");
25 String password=new BufferedReader(
26 new InputStreamReader(System.in)).readLine();
27 System.out.println(encoder.encode(password.getBytes()));
28 }
29 
30 }

 

posted on 2014-03-31 21:28  itRed  阅读(1576)  评论(0编辑  收藏  举报

导航