请问各位高手,java中有没有能实现base64编码的类,若有,请问如何使用,谢谢!!!

解决方案 »

  1.   

    javamail 中好像有 喳查吧
      

  2.   


    jdk自带 :    //base64编码
        private static sun.misc.BASE64Encoder base64encoder = new sun.misc.
                BASE64Encoder();    //base64解码
        private static sun.misc.BASE64Decoder base64decoder = new sun.misc.
                BASE64Decoder();
      

  3.   

    老大,人家都给你写出来了还看不到,sun.misc
      

  4.   

    不过据说使用以“sun”开头的类的程序可移植性差.
      

  5.   

    /**
     * This stream filter converts a stream of bytes to their Base64 encoding.
     *
     * Base64 encoding encodes 3 bytes into 4 characters.
     * |11111122|22223333|33444444|
     * 
     * Each set of 6 bits is encoded according to the toBase64 map.
     * If the number of input bytes is not a multiple of 3,
     * then the last group of 4 characters is padded with one or two = signs.
     * Each output line is at most 76 characters
     *
     */import java.io.IOException;
    import java.io.OutputStream;
    import java.io.FilterOutputStream;public class Base64OutputStream extends FilterOutputStream
    {
    private static char[] toBase64 =
    {  
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    'w', 'x', 'y', 'z', '0', '1', '2', '3',
    '4', '5', '6', '7', '8', '9', '+', '/'
    }; private int col = 0;
    private int i = 0;
    private int[] inbuf = new int[3]; /**
     * Constructors the stream filter
     *
     * @param out the stream to filter
     *
     */
    public Base64OutputStream( OutputStream out )
    {  
    super(out);
    } public void write(int c) throws IOException
    {  
    inbuf[i] = c;
    i ++;
    if (i == 3)
    {  
    super.write( toBase64[(inbuf[0] & 0xFC) >> 2] );
    super.write( toBase64[((inbuf[0] & 0x03) << 4) |
    ((inbuf[1] & 0xF0) >> 4)] );
    super.write( toBase64[((inbuf[1] & 0x0F) << 2) |
    ((inbuf[2] & 0xC0) >> 6)] );
    super.write( toBase64[inbuf[2] & 0x3F] );
    col += 4;
    i = 0;
    if (col >= 76)
    {  
    super.write('\n');
    col = 0;
    }
    }
    } public void flush() throws IOException
    {  
    if (i == 1)
    {  
    super.write( toBase64[(inbuf[0] & 0xFC) >> 2] );
    super.write( toBase64[(inbuf[0] & 0x03) << 4] );
    super.write( '=' );
    super.write( '=' );
    }
    else if (i == 2)
    {  
    super.write( toBase64[(inbuf[0] & 0xFC) >> 2] );
    super.write( toBase64[((inbuf[0] & 0x03) << 4) |
    ((inbuf[1] & 0xF0) >> 4)] );
    super.write( toBase64[(inbuf[1] & 0x0F) << 2] );
    super.write( '=' );
    }
    }
    }
      

  6.   

    使用如下:/**
     * computes the Base64 encoding of a string
     * @param s a string
     * @return the Base64 encoding of s
     */public static String base64Encode( String s ) {
            ByteArrayOutputStream bOut = new ByteArrayOutputStream();
            Base64OutputStream out = new Base64OutputStream( bOut );
            try {
                    out.write( s.getBytes() );
                    out.flush();
            }
            catch( IOException e ) { }        return bOut.toString();
    }
      

  7.   

    那装了JDK5.0,能不能用sun.misc.BASE64Encoder base64encoder 这个类?
      

  8.   

    我现在安装了javaMail,请问base64在javaMil的哪个包里,谢谢!!!
      

  9.   

    j2ee.jar包中的javax.mail.internet.MIMEUtilty这个类可以的。