一段关于BASE64编解码的代码,请高手指教

aspirerME 2005-08-22 07:32:12
编码如下:
FileInputStream fin = new FileInputStream("d://test.txt");
BufferedInputStream bin = new BufferedInputStream(fin);
DataInputStream din = new DataInputStream(bin);
String inLine = din.readLine();
String tempString = "";
while(inLine != null)
{
tempString = tempString + inLine;
inLine = din.readLine();
}
String encodedString = (new sun.misc.BASE64Encoder()).encode(tempString.getBytes());



解码如下:
public static String getFromBASE64(String src)
{
if(src == null)
{
return null;
}

try
{
byte[] b = (new sun.misc.BASE64Decoder()).decodeBuffer(src);
return new String(b);
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}

FileOutputStream fout = new FileOutputStream("c://2005.txt");
BufferedOutputStream bout = new BufferedOutputStream(fout);
DataOutputStream dout = new DataOutputStream(bout);
String s = link.getElementsByTagName("attachment").item(0).getFirstChild().getNodeValue();
// dout.writeBytes(new String(getFromBASE64(s).getBytes("ISO-8859-1"),"UTF-8"));
dout.writeBytes(getFromBASE64(s));


问题是:
我解码得到的文件是乱码,尝试了无数方法还没解决,望高手指教。

...全文
193 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qixin000 2005-11-18
  • 打赏
  • 举报
回复
原因在此:
首先你要保证你的文件时原始的系统默认格式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);
}
}


liuquanyi 2005-08-23
  • 打赏
  • 举报
回复
mark!
yyzh 2005-08-23
  • 打赏
  • 举报
回复
可能是这里错了
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);
}
}
aspirerME 2005-08-23
  • 打赏
  • 举报
回复
upup
humanity 2005-08-22
  • 打赏
  • 举报
回复
问题总是有办法解决的吧?不过我不知道。
aspirerME 2005-08-22
  • 打赏
  • 举报
回复
照楼上的说是没有办法了吗?
loveyouting 2005-08-22
  • 打赏
  • 举报
回复
在中文中用Base64编码的时候会出错..当然解的时候也会咯.....
面向工程应用:市面上的一些密码学课程和密码学的书籍,很多都是从考证出发,讲解算法原理并不面向工程应用,而我们现在缺少的是工程应用相关的知识,本课程从工程应用出发,每种技术都主要讲解其在工程中的使用,并演示工程应用的代码。 从零实现部分算法: 课程中实现了base16编解码 ,XOR对称加解密算法,PKCS7 pading数据填充算法,通过对一些简单算法的实现,从而加深对密码学的理解。理论与实践结合: 课程如果只是讲代码,同学并不能理解接口背后的原理,在项目设计中就会留下隐患,出现错误也不容易排查出问题。如果只讲理论,比如对密码学的一些研究,对于大部分从事工程应用的同学并没有必要,而是理论与实践结合,一切为了工程实践。代码现场打出: 代码不放在ppt而是现场打出,更好的让学员理解代码编写的逻辑,老师现场敲出代码正是展示出了工程项目的思考,每个步骤为什么要这么做,考虑了哪些异常,易学不枯燥: 课程为了确保大部分人开发者都学得会,理解算法原理(才能真正理解算法特性),学会工程应用(接口调用,但不局限接口调用,理解接口背后的机制,并能解决工程中会出现的问题),阅读算法源码但不实现密码算法,,并能将密码学投入到实际工程中,如果是想学习具体的加密算法实现,关注我后面的课程。

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧