急急急!!! java --- inputstream 转换为 base64

wangwenhui-com-cn 2008-12-16 06:32:10
怎么把 InputStream 流转换为 Base64 的格式呢?用Java 实现
...全文
691 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rachael1001 2008-12-20
  • 打赏
  • 举报
回复

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());
}
}

}

zhaitao81 2008-12-19
  • 打赏
  • 举报
回复
InputStream StreamObject ;//建立数据流对象
Byte[] FileByteArray=new byte[FileLength];//初始化Byte数组
StreamObject.Read (FileByteArray,0,FileLength);
string Base64String=ByteToBase.ByteToBase64(FileByteArray,0,FileByteArray.Length );
只是简单写了一下
wfeng007 2008-12-19
  • 打赏
  • 举报
回复
apache commons codec 组件包中有base64的编码实现。。。可以直接用。。。也可以把代码拷贝出来自己用。。
类应该为:org.apache.commons.codec.binary.Base64

他的参数好像是byte[] 然后返回 String。。。 你直接将String output就行了
input的话得到的String decode一下就得到byte[]然后根据需要的字符编码创建为String就可以了。
cxalxpks 2008-12-16
  • 打赏
  • 举报
回复
不懂。。关注下

67,513

社区成员

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

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