预览存储在mongoDB的文件(excel、word、PDF、图片)
来源:互联网 发布:大数据数据库设计 编辑:程序博客网 时间:2024/05/21 09:18
@RequestMapping(value = "/preview") public void preview(String attachmentId) {//获取存储在mongodb文件对应的idAttachment attachment = attachmentService.getAttachmentDetail(attachmentId); GridFSDBFile gridFSDBFile = attachmentService.getGridFSDBFile(attachment.getDbStoreId()); try { //1、EXCEL(转换为HTML预览) HSSFWorkbook workBook = new HSSFWorkbook(gridFSDBFile.getInputStream()); ExcelToHtmlConverter converter = new ExcelToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); converter.setOutputColumnHeaders(false);// 不显示列的表头 converter.setOutputRowNumbers(false);// 不显示行的表头 converter.processWorkbook(workBook); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); StreamResult streamResult = new StreamResult(outStream); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.METHOD, "html"); serializer.transform(new DOMSource(converter.getDocument()), streamResult); response.getOutputStream().write(outStream.toByteArray()); //2、word(转换为HTML预览) HWPFDocument wordDocument = new HWPFDocument(gridFSDBFile.getInputStream()); WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter( DocumentBuilderFactory.newInstance().newDocumentBuilder() .newDocument()); wordToHtmlConverter.setPicturesManager(new PicturesManager() { public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) { return "E:/test/"+suggestedName; } }); List pics = wordDocument.getPicturesTable().getAllPictures(); if (pics != null) { for (int i = 0; i < pics.size(); i++) { Picture pic = (Picture) pics.get(i); try { pic.writeImageContent(new FileOutputStream("E:/test/"+pic.suggestFullFileName())); } catch (FileNotFoundException e) { e.printStackTrace(); } } } wordToHtmlConverter.processDocument(wordDocument); Document htmlDocument = wordToHtmlConverter.getDocument(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); DOMSource domSource = new DOMSource(htmlDocument); StreamResult streamResult = new StreamResult(outStream); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.METHOD, "html"); serializer.transform(domSource, streamResult); outStream.close(); String content = new String(outStream.toByteArray()); response.getOutputStream().write(outStream.toByteArray()); //3、PDF+TXT JPG JPEG BufferedReader bis = null; BufferedInputStream br = new BufferedInputStream(gridFSDBFile.getInputStream()); response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(gridFSDBFile.getFilename(), "UTF-8")); OutputStream out = response.getOutputStream(); byte[] buf = new byte[1024]; int len = 0; while ((len = br.read(buf)) != -1) out.write(buf, 0, len); br.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
阅读全文
0 0
- 预览存储在mongoDB的文件(excel、word、PDF、图片)
- Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
- Vickate_iOS_文件预览QLPreviewController(pdf,excel,word等)
- iOS上---预览本地ppt,word ,excel,pdf文件
- ASP.NET MVC在线预览Excel、Word、TXT、PDF文件
- C#在线预览文档(word,excel,pdf,txt,png)
- ASP.NET Word Excel PDF 预览
- 如何在excel里同时插入多个对象(PDF\WORD\EXCEL\图片等)
- IOS 打开预览pdf、ppt、excel、word、tet、rtf、csv格式的文件,微信文件分享
- 笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)
- Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)
- 笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)
- 由文件的路径通过其他应用打开图片,PPT,Word,excel,PDF
- Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览word、excel、ppt、pdf文件
- ios开发 关于在app中预览pdf,word,excel时,使用QLPreviewController还是UIWebView
- C# web实现word 转Html、office转Html、pdf转图片 在线预览文件
- C# web实现word 转Html、office转Html、pdf转图片 在线预览文件
- java 在MySQL中存储文件,读取文件(包括图片,word文档,excel表格,ppt,zip文件等)
- 利用 FFmpeg 进行简单的音频拼接 降噪 合成
- Python之类的重写和私有变量
- 解决找不到Newtonsoft命名空间的问题
- js面向对象及函数原型
- Windows 下tomcat 启动多个Spring项目
- 预览存储在mongoDB的文件(excel、word、PDF、图片)
- iOS WebViewJavascriptBridge初步尝试与图文详细讲解
- 最小安装下如何打开网络连接
- javaassit
- jquery的$().each,$.each的区别
- Yarn scheduler Capacity调度器概念以及配置
- QT的动态语言切换
- ServerSocket与Socket简解
- Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]