apache commons codec 组件包中有base64的编码实现可以直接用也可以把代码拷贝出来自己用。。
类应该为:org.apache.commons.codec.binary.Base64他的参数好像是byte[] 然后返回 String 你直接将String output就行了
input的话得到的String decode一下就得到byte[]然后根据需要的字符编码创建为String就可以了。

解决方案 »

  1.   

    InputStream StreamObject ;//建立数据流对象
    Byte[] FileByteArray=new byte[FileLength];//初始化Byte数组
    StreamObject.Read (FileByteArray,0,FileLength);
    string Base64String=ByteToBase.ByteToBase64(FileByteArray,0,FileByteArray.Length );
    只是简单写了一下
      

  2.   


    package com.jmp.common;import java.io.*;
    import sun.misc.*;
    public class PicEncodeDecodeBase64
    {
        private String base64str=null;//base64编码字符串
        private String picname="d:/temp1/002.jpg";//目标图片
        FileInputStream file;
        public sun.misc.BASE64Encoder encode=new sun.misc.BASE64Encoder();
        
        public PicEncodeDecodeBase64()
        {
        //    System.out.println("init");
        }
        public String Read()
        {
            int n=0;
            try{
                file=new FileInputStream(picname);    
                while((n=file.available())>0)
                {
                    byte[] b=new byte[n];
                    int result=file.read(b);
                    if(result==-1)break;
                    base64str=new String(b);                
                }
                //System.out.println(base64str);
                base64str=encode.encode(base64str.getBytes());    //得到base64编码串        
            }
            catch(Exception e)
            {
                System.out.println("read pic file error");
            }
            return base64str;
        }    public void Write(String photoName,String baseCode64,String savePath)
        {
                try{
                //String a=null;
                //FileOutputStream fo=new FileOutputStream("c:/idcard.jpg");
                String file = savePath+photoName;
                FileOutputStream fo=new FileOutputStream(file);//还原后的图片名
                sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();//base64解码类
                //base64str = baseCode64;
                //System.out.println("base64str="+base64str);
                byte[] b = decoder.decodeBuffer(baseCode64);            
                fo.write(b);//写入图片文件
                
                //System.out.write('\t');
                fo.flush();
                }
                catch(Exception e)
                {
                    System.out.println("write method error is " + e.toString());
                }
        }
        
    }