背景
最近公司内部oa系统升级,需要增加文件在线预览服务,最常见的文件就是office文档,一开始构思几个方案,比如office软件自带的文件转换,openoffice转换,offce365服务,aspose组件转换,最终采用了aspose转换,原因是组件功能完善,不依赖其它软件安装环境
系统设计
文件类型及方案
注:aspose因版权问题,工程示例代码中全部使用试用版,转换图片会出现水印
流程设计
系统实现
识别文件后缀
URL指向文件真实路径时根据后缀名判断
publicstaticStringgetTypeByExtenssion(StringlinkUrl){if(linkUrl==null)returnnull;linkUrl=linkUrl.toLowerCase();for(Stringext:extensions){if(linkUrl.endsWith(ext)){returnext;}}returnnull;}
URL为文件输出流时
根据文件输出流的disposition
privatestaticStringgetTypeByDisposition(Stringdisposition){Stringext=null;if(!StringUtils.isEmpty(disposition)){disposition=StringUtils.replace(disposition,"\"","");String[]strs=disposition.split(";");for(Stringstring:strs){if(string.toLowerCase().indexOf("filename=")>=0){ext=StringUtils.substring(string,string.lastIndexOf("."));break;}}}returnext;}
根据文件输出流content-type
types=newHashMap<String,String>();types.put("application/pdf",".pdf");types.put("application/msword",".doc");types.put("text/plain",".txt");types.put("application/javascript",".js");types.put("application/x-xls",".xls");types.put("application/-excel",".xls");types.put("text/html",".html");types.put("application/x-rtf",".rtf");types.put("application/x-ppt",".ppt");types.put("image/jpeg",".jpg");types.put("application/vnd.openxmlformats-officedocument.wordprocessingml.template",".docx");types.put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",".xlsx");types.put("application/vnd.openxmlformats-officedocument.presentationml.presentation",".pptx");types.put("message/rfc822",".eml");types.put("application/xml",".xml");
根据stream的固定字节判断
FILE_TYPE_MAP.put(".pdf","255044462D312E");//AdobeAcrobat(pdf)FILE_TYPE_MAP.put(".doc","D0CF11E0");//MSWordFILE_TYPE_MAP.put(".xls","D0CF11E0");//MSExcel注意:word和excel的文件头一样FILE_TYPE_MAP.put(".jpg","FFD8FF");//JPEG(jpg)FILE_TYPE_MAP.put(".png","89504E47");//PNG(png)FILE_TYPE_MAP.put(".gif","47494638");//GIF(gif)FILE_TYPE_MAP.put(".tif","49492A00");//TIFF(tif)FILE_TYPE_MAP.put(".bmp","424D");//WindowsBitmap(bmp)FILE_TYPE_MAP.put(".dwg","41433130");//CAD(dwg)FILE_TYPE_MAP.put(".html","68746D6C3E");//HTML(html)FILE_TYPE_MAP.put(".rtf","7B5C727466");//RichTextFormat(rtf)FILE_TYPE_MAP.put(".xml","3C3F786D6C");FILE_TYPE_MAP.put(".zip","504B0304");//docx的文件头与zip的一样FILE_TYPE_MAP.put(".rar","52617221");FILE_TYPE_MAP.put(".psd","38425053");//Photoshop(psd)FILE_TYPE_MAP.put(".eml","44656C69766572792D646174653A");//EmailFILE_TYPE_MAP.put(".dbx","CFAD12FEC5FD746F");//OutlookExpress(dbx)FILE_TYPE_MAP.put(".pst","2142444E");//Outlook(pst)FILE_TYPE_MAP.put(".mdb","5374616E64617264204A");//MSAccess(mdb)FILE_TYPE_MAP.put(".wpd","FF575043");//WordPerfect(wpd)FILE_TYPE_MAP.put(".eps","252150532D41646F6265");FILE_TYPE_MAP.put(".ps","252150532D41646F6265");FILE_TYPE_MAP.put(".qdf","AC9EBD8F");//Quicken(qdf)FILE_TYPE_MAP.put(".pwl","E3828596");//WindowsPassword(pwl)FILE_TYPE_MAP.put(".wav","57415645");//Wave(wav)FILE_TYPE_MAP.put(".avi","41564920");FILE_TYPE_MAP.put(".ram","2E7261FD");//RealAudio(ram)FILE_TYPE_MAP.put(".rm","2E524D46");//RealMedia(rm)FILE_TYPE_MAP.put(".mpg","000001BA");//FILE_TYPE_MAP.put(".mov","6D6F6F76");//Quicktime(mov)FILE_TYPE_MAP.put(".asf","3026B2758E66CF11");//WindowsMedia(asf)FILE_TYPE_MAP.put(".mid","4D546864");//MIDI(mid)
文件解析
word分页转换图片
Documentdoc=newDocument(fileConvertInfo.getFilePath());for(inti=0;i<doc.getPageCount();i++){DocumentextractedPage=doc.extractPages(i,1);extractedPage.save(fileConvertInfo.getFileDirPath()+"split_"+(i+1)+".jpeg",SaveFormat.JPEG);}
ppt分页转换图片
Presentationppt=newPresentation(fileConvertInfo.getFilePath());for(inti=0;i<ppt.getSlides().size();i++){ISlideslide=ppt.getSlides().get_Item(i);intheight=(int)(ppt.getSlideSize().getSize().getHeight()-150);intwidth=(int)(ppt.getSlideSize().getSize().getWidth()-150);BufferedImageimage=slide.getThumbnail(newjava.awt.Dimension(width,height));//每一页输出一张图片FileoutImage=newFile(fileConvertInfo.getFileDirPath()+"split_"+(i+1)+".jpeg");ImageIO.write(image,"jpeg",outImage);}
excel转换html
Workbookwb=newWorkbook(fileConvertInfo.getFilePath());HtmlSaveOptionsopts=newHtmlSaveOptions();opts.setExportWorksheetCSSSeparately(true);opts.setExportSimilarBorderStyle(true);Worksheetws=wb.getWorksheets().get(0);wb.save(fileConvertInfo.getFileDirPath()+"convert.html",opts);
excel分页转换图片(另一种预览方式)
Workbookwb=newWorkbook(fileConvertInfo.getFilePath());ImageOrPrintOptionsimgOptions=newImageOrPrintOptions();imgOptions.setImageFormat(ImageFormat.getJpeg());for(inti=0;i<wb.getWorksheets().getCount();i++){Worksheetsheet=wb.getWorksheets().get(i);SheetRendersr=newSheetRender(sheet,imgOptions);sr.toImage(i,fileConvertInfo.getFileDirPath()+"split_"+(i+1)+".jpeg");}
pdf分页转换图片
PDDocumentpdf=PDDocument.load(newFile((fileConvertInfo.getFilePath())));intpageCount=pdf.getNumberOfPages();PDFRendererrenderer=newPDFRenderer(pdf);for(inti=0;i<pageCount;i++){BufferedImageimage=renderer.renderImage(i,1.25f);//第二个参数越大生成图片分辨率越高,转换时间也就越长ImageIO.write(image,"JPEG",newFile(fileConvertInfo.getFileDirPath()+"split_"+(i+1)+".jpeg"));}pdf.close();
预览图片
<body><divid="app"><imgid="image"style="display:none"></div></body></html><script>$(function(){$("#image").attr("src",getQueryString("file"));varimage=newViewer(document.getElementById('image'),{url:'data-original',button:false,navbar:false,backdrop:false});document.getElementById('image').click();})</script>
预览视频
privatestaticStringgetTypeByDisposition(Stringdisposition){Stringext=null;if(!StringUtils.isEmpty(disposition)){disposition=StringUtils.replace(disposition,"\"","");String[]strs=disposition.split(";");for(Stringstring:strs){if(string.toLowerCase().indexOf("filename=")>=0){ext=StringUtils.substring(string,string.lastIndexOf("."));break;}}}returnext;}0
系统效果
使用方法
直接运行项目,输入预览地址
http://localhost:8098/fastpreview/plaform/index.html?file=(文件地址)
文件地址支持文件访问路径与流输出
项目源码地址
https://gitee.com/code2roc/fast-preview
预览界面
word
excel
ppt
image
vedio
txt