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