简易实现, txt文件显示在html上
来源:互联网 发布:mac鼠标滚轮设置 编辑:程序博客网 时间:2024/05/16 09:37
为了应对客户的各种附件需要,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(); } } }
- 简易实现, txt文件显示在html上
- 在手机上显示项目html文件
- 实现在windows7上部署HTML文件
- 读取txt文件显示到treeview上
- PHP实现HTML批量转TXT文件
- 每隔一秒读取txt文件显示在jsp页面上
- 读取.txt文件并在页面显示
- 在Mac上建.txt文件
- HTML--在页面上显示html标签
- 在LCD上实现简易电子钟
- txt文件解析简易框架
- 在浏览器中显示TXT 文本中的换行(html/txt)
- 在浏览器中显示TXT 文本中的换行(html/txt)
- 在浏览器中显示TXT 文本中的换行(html/txt)
- 在网页上显示html代码
- 在WebView上正确显示HTML内容
- processing如何在html上运行显示
- emoji表情在web html上显示
- bundle开发----数据库访问(基于MySQL)
- PowerDesigner画ER图详细教程
- MSP430F149利用JTAG以及BSL下载配置图解!
- 网站头部回弹广告js代码
- C++类中的有元理解
- 简易实现, txt文件显示在html上
- QT 强止杀进程
- visual studio 2010 又学一招
- 正则表达式口诀及教程
- oracle异常:ORA-27101: shared memory realm does not exist ;ORA-03113: 通信通道的文件结尾(错误解决)
- jQuery-强大的jQuery选择器 (详解)
- VC强止终止进程
- Eclipse快捷键大全
- 数据库安全设置