我这个有个类 好似是Base64 你能不能告诉我,这个是实现什么的啊?

解决方案 »

  1.   

    Base64位编码的代码 是先什么或者你帮我解释一下这个名词!我好像有类似的文件!
      

  2.   


    顶顶
    import sun.misc.BASE64Encoder;
    import sun.misc.BASE64Decoder;
    也可以的
      

  3.   

    import sun.misc.BASE64Encoder; 
    import sun.misc.BASE64Decoder;
    不知道是不是按位实现的 没去看过,下面一份是按位的。很长发不出来 有邮箱吗?
      

  4.   

    看看这个!http://pmjava.com/blogview.asp?id=349
      

  5.   

    下面这个是 Apache Commons Codec Base64 SVN 上的源代码http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java?view=up
      

  6.   


    package com.smtp;import java.io.*;public class Base64
    { private static final char S_BASE64CHAR[] = {
    '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 static final byte S_DECODETABLE[]; static 
    {
    S_DECODETABLE = new byte[128];
    for (int i = 0; i < S_DECODETABLE.length; i++)
    S_DECODETABLE[i] = 127; for (int i = 0; i < S_BASE64CHAR.length; i++)
    S_DECODETABLE[S_BASE64CHAR[i]] = (byte)i; }
    private static int decode0(char ibuf[], byte obuf[], int wp)
    {
    int outlen = 3;
    if (ibuf[3] == '=')
    outlen = 2;
    if (ibuf[2] == '=')
    outlen = 1;
    int b0 = S_DECODETABLE[ibuf[0]];
    int b1 = S_DECODETABLE[ibuf[1]];
    int b2 = S_DECODETABLE[ibuf[2]];
    int b3 = S_DECODETABLE[ibuf[3]];
    switch (outlen)
    {
    case 1: // '\001'
    obuf[wp] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 3);
    return 1; case 2: // '\002'
    obuf[wp++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 3);
    obuf[wp] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
    return 2; case 3: // '\003'
    obuf[wp++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 3);
    obuf[wp++] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
    obuf[wp] = (byte)(b2 << 6 & 0xc0 | b3 & 0x3f);
    return 3;
    }
    throw new RuntimeException("Couldn't decode.");
    } public static byte[] decode(char data[], int off, int len)
    {
    char ibuf[] = new char[4];
    int ibufcount = 0;
    byte obuf[] = new byte[(len / 4) * 3 + 3];
    int obufcount = 0;
    for (int i = off; i < off + len; i++)
    {
    char ch = data[i];
    if (ch != '=' && (ch >= S_DECODETABLE.length || S_DECODETABLE[ch] == 127))
    continue;
    ibuf[ibufcount++] = ch;
    if (ibufcount == ibuf.length)
    {
    ibufcount = 0;
    obufcount += decode0(ibuf, obuf, obufcount);
    }
    } if (obufcount == obuf.length)
    {
    return obuf;
    } else
    {
    byte ret[] = new byte[obufcount];
    System.arraycopy(obuf, 0, ret, 0, obufcount);
    return ret;
    }
    } public static byte[] decode(String data)
    {
    char ibuf[] = new char[4];
    int ibufcount = 0;
    byte obuf[] = new byte[(data.length() / 4) * 3 + 3];
    int obufcount = 0;
    for (int i = 0; i < data.length(); i++)
    {
    char ch = data.charAt(i);
    if (ch != '=' && (ch >= S_DECODETABLE.length || S_DECODETABLE[ch] == 127))
    continue;
    ibuf[ibufcount++] = ch;
    if (ibufcount == ibuf.length)
    {
    ibufcount = 0;
    obufcount += decode0(ibuf, obuf, obufcount);
    }
    } if (obufcount == obuf.length)
    {
    return obuf;
    } else
    {
    byte ret[] = new byte[obufcount];
    System.arraycopy(obuf, 0, ret, 0, obufcount);
    return ret;
    }
    } public static void decode(char data[], int off, int len, OutputStream ostream)
    throws IOException
    {
    char ibuf[] = new char[4];
    int ibufcount = 0;
    byte obuf[] = new byte[3];
    for (int i = off; i < off + len; i++)
    {
    char ch = data[i];
    if (ch != '=' && (ch >= S_DECODETABLE.length || S_DECODETABLE[ch] == 127))
    continue;
    ibuf[ibufcount++] = ch;
    if (ibufcount == ibuf.length)
    {
    ibufcount = 0;
    int obufcount = decode0(ibuf, obuf, 0);
    ostream.write(obuf, 0, obufcount);
    }
    } } public static void decode(String data, OutputStream ostream)
    throws IOException
    {
    char ibuf[] = new char[4];
    int ibufcount = 0;
    byte obuf[] = new byte[3];
    for (int i = 0; i < data.length(); i++)
    {
    char ch = data.charAt(i);
    if (ch != '=' && (ch >= S_DECODETABLE.length || S_DECODETABLE[ch] == 127))
    continue;
    ibuf[ibufcount++] = ch;
    if (ibufcount == ibuf.length)
    {
    ibufcount = 0;
    int obufcount = decode0(ibuf, obuf, 0);
    ostream.write(obuf, 0, obufcount);
    }
    } } public static String encode(byte data[])
    {
    return encode(data, 0, data.length);
    } public static String encode(byte data[], int off, int len)
    {
    if (len <= 0)
    return "";
    char out[] = new char[(len / 3) * 4 + 4];
    int rindex = off;
    int windex = 0;
    int rest;
    for (rest = len - off; rest >= 3; rest -= 3)
    {
    int i = ((data[rindex] & 0xff) << 16) + ((data[rindex + 1] & 0xff) << 8) + (data[rindex + 2] & 0xff);
    out[windex++] = S_BASE64CHAR[i >> 18];
    out[windex++] = S_BASE64CHAR[i >> 12 & 0x3f];
    out[windex++] = S_BASE64CHAR[i >> 6 & 0x3f];
    out[windex++] = S_BASE64CHAR[i & 0x3f];
    rindex += 3;
    } if (rest == 1)
    {
    int i = data[rindex] & 0xff;
    out[windex++] = S_BASE64CHAR[i >> 2];
    out[windex++] = S_BASE64CHAR[i << 4 & 0x3f];
    out[windex++] = '=';
    out[windex++] = '=';
    } else
    if (rest == 2)
    {
    int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
    out[windex++] = S_BASE64CHAR[i >> 10];
    out[windex++] = S_BASE64CHAR[i >> 4 & 0x3f];
    out[windex++] = S_BASE64CHAR[i << 2 & 0x3f];
    out[windex++] = '=';
    }
    return new String(out, 0, windex);
    } public static void encode(byte data[], int off, int len, OutputStream ostream)
    throws IOException
    {
    if (len <= 0)
    return;
    byte out[] = new byte[4];
    int rindex = off;
    int rest;
    for (rest = len - off; rest >= 3; rest -= 3)
    {
    int i = ((data[rindex] & 0xff) << 16) + ((data[rindex + 1] & 0xff) << 8) + (data[rindex + 2] & 0xff);
    out[0] = (byte)S_BASE64CHAR[i >> 18];
    out[1] = (byte)S_BASE64CHAR[i >> 12 & 0x3f];
    out[2] = (byte)S_BASE64CHAR[i >> 6 & 0x3f];
    out[3] = (byte)S_BASE64CHAR[i & 0x3f];
    ostream.write(out, 0, 4);
    rindex += 3;
    } if (rest == 1)
    {
    int i = data[rindex] & 0xff;
    out[0] = (byte)S_BASE64CHAR[i >> 2];
    out[1] = (byte)S_BASE64CHAR[i << 4 & 0x3f];
    out[2] = 61;
    out[3] = 61;
    ostream.write(out, 0, 4);
    } else
    if (rest == 2)
    {
    int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
    out[0] = (byte)S_BASE64CHAR[i >> 10];
    out[1] = (byte)S_BASE64CHAR[i >> 4 & 0x3f];
    out[2] = (byte)S_BASE64CHAR[i << 2 & 0x3f];
    out[3] = 61;
    ostream.write(out, 0, 4);
    }
    } public static void encode(byte data[], int off, int len, Writer writer)
    throws IOException
    {
    if (len <= 0)
    return;
    char out[] = new char[4];
    int rindex = off;
    int rest = len - off;
    int output = 0;
    do
    {
    if (rest < 3)
    break;
    int i = ((data[rindex] & 0xff) << 16) + ((data[rindex + 1] & 0xff) << 8) + (data[rindex + 2] & 0xff);
    out[0] = S_BASE64CHAR[i >> 18];
    out[1] = S_BASE64CHAR[i >> 12 & 0x3f];
    out[2] = S_BASE64CHAR[i >> 6 & 0x3f];
    out[3] = S_BASE64CHAR[i & 0x3f];
    writer.write(out, 0, 4);
    rindex += 3;
    rest -= 3;
    if ((output += 4) % 76 == 0)
    writer.write("\n");
    } while (true);
    if (rest == 1)
    {
    int i = data[rindex] & 0xff;
    out[0] = S_BASE64CHAR[i >> 2];
    out[1] = S_BASE64CHAR[i << 4 & 0x3f];
    out[2] = '=';
    out[3] = '=';
    writer.write(out, 0, 4);
    } else
    if (rest == 2)
    {
    int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
    out[0] = S_BASE64CHAR[i >> 10];
    out[1] = S_BASE64CHAR[i >> 4 & 0x3f];
    out[2] = S_BASE64CHAR[i << 2 & 0x3f];
    out[3] = '=';
    writer.write(out, 0, 4);
    }
    }
    public static void main(String[] a){
    byte[] s = "nihao".getBytes();

       String result = Base64.encode(s);
       System.out.println("result="+result);
      byte[] yuan =  Base64.decode(result);
      String p = new String(yuan);
      System.out.println("p+"+p);
    }

    }
      

  7.   

    import sun.misc.BASE64Encoder;BASE64Encoder be=new BASE64Encoder();//对图片进行base64编码
    FileInputStream fis=new FileInputStream(request.getRealPath("/")+"web\\valuechain\\image\\"+filename);
    byte bt[]=new byte[fis.available()];
    fis.read(bt,0,fis.available());
    fs.close();
    String picbyte=be.encode(bt).toString();这是我对图片进行的base64编码。。在.mht中用的
      

  8.   

    抱歉,我也不知道。
    我在做一个东西需要用到Base64位编码。
      

  9.   

    [email protected]
    谢谢你。