文件编码

package mytest;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class FileOuttest {

public static String file2Str(String file){
//将文件转化为字节数组字符串,并对其进行Base64编码处理
InputStream in = null;
byte[] data = null;
//读取文件字节数组
BASE64Encoder Base64 = new BASE64Encoder();
try{
in = new FileInputStream(file);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(Base64.encode(data).toString());
return new String(Base64.encode(data));

}


//public static void main(String[] args) {
// file2Str("C:\\Users\\mz\\Desktop\\过滤面板代码.txt");
//}
//
}

bos中调用 附件管理功能上传附件

package com.kingdee.eas.custom.app;

import org.apache.log4j.Logger;

import sun.misc.BASE64Decoder;

import javax.ejb.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.rmi.RemoteException;
import com.kingdee.bos.*;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.rule.RuleExecutor;
import com.kingdee.bos.metadata.MetaDataPK; //import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.framework.ejb.AbstractEntityControllerBean;
import com.kingdee.bos.framework.ejb.AbstractBizControllerBean; //import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.service.ServiceContext;
import com.kingdee.bos.service.IServiceContext;
import com.kingdee.eas.base.attachment.common.AttachmentManagerFactory;
import com.kingdee.eas.base.attachment.common.AttachmentServerManager;
import com.kingdee.eas.base.attachment.common.SimpleAttachmentInfo;
import com.kingdee.eas.common.EASBizException;

public class SaleattachFileFacadeControllerBean extends
AbstractSaleattachFileFacadeControllerBean {
private static Logger logger = Logger
.getLogger("com.kingdee.eas.custom.app.SaleattachFileFacadeControllerBean");

@Override
protected boolean _fileUpload(Context ctx, String id, String fileStr,
String fileName, String fileType) throws BOSException,
EASBizException {
try {
//上传附件测试------------------------------------------------------------
BASE64Decoder Base64 = new BASE64Decoder();
// try {
// Base64解码
byte[] b = Base64.decodeBuffer(fileStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成文件,并保存在服务器硬盘上
OutputStream out = new FileOutputStream(
"C:\\Users\\mz\\Desktop\\新建文件夹\\"+fileName+"."+fileType+"");
out.write(b);
out.flush();
out.close();
String easPath = "C:\\Users\\mz\\Desktop\\新建文件夹\\"+fileName+"."+fileType+"";
File file = new File(easPath);
//String extName = fileName.substring(fileName.lastIndexOf(".") + 1);
System.out.println("eas附件路径:" + easPath + ",extName=" + fileType);
AttachmentServerManager serverManager = AttachmentManagerFactory
.getServerManager(ctx);
SimpleAttachmentInfo saInfo = new SimpleAttachmentInfo();
saInfo.setExtName(fileType);
saInfo.setMainName(fileName);
saInfo.setContent(b);
serverManager.addNewAttachment(ctx,id,saInfo);
file.delete();//把上传到临时目录的附件删掉
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}


}

}