mongodb 下载word 二进制流文件 转出html文件

版权声明:本文为博主原创烂文章,你爱转载就转载 https://blog.csdn.net/qq_38623459/article/details/84315117

@RequestMapping(value="/getyjpllook",produces="application/json;charset=UTF-8")
    public  void getyjpllook(String plid)throws Throwable {
        
        String filepath = "C:/html";
        File file1=new File(filepath);
        if(!file1.exists()){  
            file1.mkdir();  
        }  
        String picturesPath = filepath+"/image/";
        File picturesDir = new File(picturesPath);
        if(!picturesDir.exists()){  
            picturesDir.mkdir();  
        } 
        String content = null;
        //InputStream in =new FileInputStream(new File(docFile));
        TbfileSelect tbfileSelect = new TbfileSelect();
        tbfileSelect.setPlanid(plid);
        List<Tbfile> filelist = tbfileservice.getTbfileListByplanid(tbfileSelect);
        ByteAndInfo bi = gsMongoClient.getByteAndInfo(InDomainCommConst.PLAN_NAME,filelist.get(0).getId());
        ByteArrayInputStream input = new ByteArrayInputStream(bi.getByteFile());
        String[] strArray = bi.getName().split("\\.");
        if(strArray[strArray.length -1].equals("docx")){
            XWPFDocument document = new XWPFDocument(input);
            XHTMLOptions options = XHTMLOptions.create();
            options.setExtractor(new FileImageExtractor(picturesDir));
            options.URIResolver(new BasicURIResolver(picturesPath));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            XHTMLConverter.getInstance().convert(document, baos, options);
            baos.close();
            content = baos.toString();
        }else{
            HWPFDocument wordDocument =new HWPFDocument(input);
            WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
            wordToHtmlConverter.setPicturesManager(new PicturesManager(){
                @Override
                public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches,
                        float heightInches) {
                    File file = new File(picturesPath + suggestedName);
                    FileOutputStream fos = null;
                    try{
                        fos = new FileOutputStream(file);
                        fos.write(content);
                        fos.close();
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                    return picturesPath + suggestedName;
                }
            });
            wordToHtmlConverter.processDocument(wordDocument);
            org.w3c.dom.Document htmlDocument = wordToHtmlConverter.getDocument();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            DOMSource domSource = new DOMSource(htmlDocument);
            StreamResult streamResult = new StreamResult(out);
            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);
              out.close();
              content = new String(out.toByteArray());
        }
        File fileyl = new File("C:/html");
        if(fileyl.exists()) {//删除原来的旧文件
         fileyl.delete();
        }
        FileUtils.writeStringToFile(new File("C:/html", "预览.html"), content, "utf-8");
       // File file = new File("预览.html");
        //Runtime ce=Runtime.getRuntime();
      //  ce.exec("cmd /c start "+fileyl.getAbsolutePath()+"/预览.html");//

猜你喜欢

转载自blog.csdn.net/qq_38623459/article/details/84315117