预览存储在mongoDB的文件(excel、word、PDF、图片)

@RequestMapping(value = "/preview")
    public void preview(String attachmentId) {
//获取存储在mongodb文件对应的id
Attachment 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();
            }
        }

你可能感兴趣的:(文件预览)