txt文件在html页面显示,简易实现, txt文件显示在html上

this.txtViewForm.value = content;

/**

* 综合利用,在线预览 txt格式附件

* @param osName

* @param docId

* @throws EcmException

* @throws IOException

*/

public static String downloadPrimaryAttach(String osName, String docId) throws EcmException, IOException {

EntityManager em = null;

try {

em = DaoManager.getEntityManager();

ObjectStore os = ObjectStoreHelper.getObjectStore(osName);

Document doc = P8DocumentDao.fetchDocumentById(os, docId);

Collection collection = EngineCollectionUtils.c(doc.get_ContentElements(), ContentTransfer.class);

Iterator it = collection.iterator();

ContentTransfer content = it.next();

InputStream in = content.accessContentStream();

StringBuffer sb = new StringBuffer();

//根据特征码,来判断文本文件编码格式,目前版本不支持全部格式转换

//在网络上查询资料,没有找到可用的覆盖面比较广的特征码集合。求各位大神分享。

byte[] head = new byte[3];

in.read(head);

String code = "GBK";

if (head[0] == -1 && head[1] == -2 ) {

//txt编码为utf8

code = "UTF-8";

}else if (head[0] == -2 && head[1] == -1 ){

//txt编码为Unicode

code = "UTF-16";

}else if(head[0]==-17 && head[1]==-69 && head[2] ==-65){

//txt编码为UTF-8

code = "UTF-8";

}else if(head[0]==49 && head[1]==46 && head[2] ==-44){

//[49, 46, -44]

code ="GB2312";

}

Charset charset = Charset.forName(code);

//此处需要设置charset,以便很好的控制页面上文本的乱码问题。

InputStreamReader reader = new InputStreamReader(in,charset);

BufferedReader bfreader = new BufferedReader(reader);

String content1 = "";

while(content1 != null){

content1 = bfreader.readLine();

if(content1 == null){

break;

}

sb.append(content1.trim());

sb.append("\r\n");

}

return sb.toString();

} catch (Exception e) {

logger.error("downloadAttach error", e);

throw new EcmException(ErrorCode.ATTACHMENT_DOWNLOAD_ERROR, "downloadAttach error ", e);

} finally {

if (em != null) {

em.close();

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值