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

    为了应对客户的各种附件需要,office、pdf都通过第三方控件来处理(在线预览),而常见的text格式和imag,就要自己来实现。
text格式附件的预览,其实就是通过java io把附件数据读出来,然后把字符流返回给页面就可以了。
    比如,页面上可以用textarea控件来render附件的内容(string),如果没有太严格的权限限制(附件内容“完全只读”,需要禁用右键复制,黏贴等功能),我们只用textarea的readonly属性来控制是否只读即可。

    页面代码:
    <textarea data-dojo-attach-point="txtViewForm" style="font-ize: 15px; width: 100%;height: 100%"  readonly="readonly" >
    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<ContentTransfer> collection = EngineCollectionUtils.c(doc.get_ContentElements(), ContentTransfer.class);
            Iterator<ContentTransfer> 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();
            }
        }
    }







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是一道光_

1分也是爱,鼓励鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值