在中文中用Base64编码的时候会出错..当然解的时候也会咯.....

解决方案 »

  1.   

    可能是这里错了
    while(inLine != null)
    {
      tempString = tempString + inLine;
      inLine = din.readLine();
    }我手写一段代码,大概的意思,有错误你再修改//编码
    FileInputStream fin = new FileInputStream("d://test.txt");
    int len=0;
    byte buf[]=new byte[128];
    String tempString = null;
    if (fin!=null)[
    while ((len=fin.read(buf))!=-1){
    tempString.write(buf,0,len);
    }
    }
      

  2.   

    原因在此:
    首先你要保证你的文件时原始的系统默认格式ANSI,这样里面的中文不会有问题但是如果你的文本文件被另存为了utf-8格式的,那个用你的读取文件的方法,就有问题了,因为读到内存里的信息本身就是乱码,当然解析回来的也是乱码,这不是base64的问题,是因为你的文件的存储格式问题如何解决
    如果你的文件是utf8格式的,就不要用你的方法读取了,要以utf8的格式来读取文件(另外你读取文件的方法有些都过时了,不要再这样读取文件了)package com;import java.io.*;/**
     * Created by qixin.
     * User: qixin
     * Date: 2005-11-15
     * Time: 12:09:29
     */
    public class aa {
        public String getEncodeInfo(String filename) throws IOException {
            InputStreamReader streamReader = new InputStreamReader(new FileInputStream(filename),"utf-8");
            BufferedReader reader = new BufferedReader(streamReader);
            String inLine = reader.readLine();
            String tempString = "";
            while (inLine != null) {
                tempString = tempString + inLine;
                inLine = reader.readLine();
            }
            return tempString;
        }    public void saveEncodeInfo(String tempString, String outputFilename) throws IOException {
            System.out.println("tempString = " + tempString);
            String encodedString = (new sun.misc.BASE64Encoder()).encode(tempString.getBytes());
            System.out.println("encodedString = " + encodedString);
            BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilename));
            writer.write(encodedString);
            writer.flush();
            writer.close();
        }
        public String getFromBASE64(String filename) throws IOException {
            String info = this.getEncodeInfo(filename);
            if (info.equals("")) {
                return null;
            }        try {
                byte[] b = (new sun.misc.BASE64Decoder()).decodeBuffer(info);
                return new String(b);
            }
            catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }    public static void main(String[] args) throws IOException {
            aa a = new aa();
            String info = a.getEncodeInfo("c://test.txt");
            a.saveEncodeInfo(info,"c://test1.txt");
            String info1 = a.getFromBASE64("c://test1.txt");
            System.out.println("info1 = " + info1);
        }
    }