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

本文介绍了一个将.docx和.doc文件转换为HTML预览的方法,包括使用XWPFDocument和HWPFDocument读取文件,以及如何处理图片并保存到指定目录。通过这种方式,可以将Word文档内容转换为网页进行预览。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@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");//

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值