doc转html并图片转base64

package com.lly.demo.util;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

import com.lly.demo.bean.DocUser;
import com.spire.doc.FileFormat;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import static java.lang.System.out;


//word转html
public class docTohtml {


    public static void main(String[] args) throws Exception {
        docxToHtml();
    }
    public Boolean docTohtml(DocUser docUser) throws Exception {
        String sourceFileName = docUser.getDocname();
        String targetFileName = docUser.getHtmlname();
        String imagePathStr = "D:/doc2htmltest/image/";
        Boolean secORfiled=true;
        HWPFDocument wordDocument = null;
        File file=new File(sourceFileName);
        FileInputStream fileInputStream = new FileInputStream(sourceFileName);
        String docxtodocSourceFileName = docUser.getDocname().substring(0,docUser.getDocname().length()-5)+".doc";
        try {
            if(StringUtils.isNotBlank(sourceFileName)&&StringUtils.isNotBlank(targetFileName)){
                    if(sourceFileName.endsWith(".doc")){

                        if(file.exists()){

                            out.println("aaaaa11111");
                            wordDocument = new HWPFDocument(fileInputStream);
                            out.println("aaaaa");
                            if (wordDocument != null) {
                                WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
                                wordToHtmlConverter.setPicturesManager((a, b, suggestedName, d, e) -> {
                                    // convertFileToBase64()
                                    out.println(suggestedName);

                                    //返回图片路径
                                    //return "image" + File.separator + suggestedName;
                                    //返回图片base64值
                                    return "data:image/"+b.getExtension().toLowerCase()+";base64,"+new String(Base64.encodeBase64(a));
                                });
                                wordToHtmlConverter.processDocument(wordDocument);
                                List pics = wordDocument.getPicturesTable().getAllPictures();
                                out.println(pics);
                                Iterator var8 = pics.iterator();

                                //转换图片
                                imgToBase64 imgToBase64pl=new imgToBase64();
                                int i=1;
                                while(var8.hasNext()) {
                                    Picture pic = (Picture)var8.next();
                                    //下载图片
                                    // pic.writeImageContent(new FileOutputStream(imagePathStr + pic.suggestFullFileName()));
                                    //System.out.println("第"+i++ +":"+imgToBase64pl.convertFileToBase64(imagePathStr + pic.suggestFullFileName()));
                                }

                                Document htmlDocument = wordToHtmlConverter.getDocument();
                                //生成空文档
                                DOMSource domSource = new DOMSource(htmlDocument);
                                //获取路径下html
                                StreamResult streamResult = new StreamResult(new File(targetFileName));
                                //设置html文件规范
                                TransformerFactory tf = TransformerFactory.newInstance();
                                Transformer serializer = tf.newTransformer();
                                serializer.setOutputProperty("encoding", "utf-8");
                                serializer.setOutputProperty("indent", "yes");
                                serializer.setOutputProperty("method", "html");
                                serializer.transform(domSource, streamResult);
                            }else {
                                out.println("请检查文件类型或后缀是否正确!");
                                secORfiled=false;
                            }

                        }else{
                            out.println("文件不存在");
                            secORfiled=false;
                        }

                    }else if(sourceFileName.endsWith("docx")){
                        System.out.println("上传文件为docx类型");
                        secORfiled=false;
                    }else{
                        out.println("上传文件为其他类型文件");
                        secORfiled=false;
                    }

            }else{
                secORfiled=false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            secORfiled=false;
        }finally {
            fileInputStream.close();
            return secORfiled;
        }


    }

    public static void docxToHtml() throws Exception {

        //D:\zpdtolly\工作总结文档\zpd使用文档\v4\用户使用手册\客户端使用手册
        String sourceFileName = "D:\\zpdtolly\\工作总结文档\\zpd使用文档\\uos+ql飞腾\\uos+麒麟飞腾使用手册-网络版.doc";
        String targetFileName = "D:\\zpdtolly\\工作总结文档\\zpd使用文档\\uos+ql飞腾\\uos+麒麟飞腾使用手册-网络版.html";
        String imagePathStr = "D:/doc2htmltest/image/";
        HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(sourceFileName));
        WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
        wordToHtmlConverter.setPicturesManager((a, b, suggestedName, d, e) -> {
           // convertFileToBase64()
            out.println(suggestedName);

            //返回图片路径
            //return "image" + File.separator + suggestedName;
            //返回图片base64值
            return "data:image/"+b.getExtension().toLowerCase()+";base64,"+new String(Base64.encodeBase64(a));
        });
        wordToHtmlConverter.processDocument(wordDocument);
        List pics = wordDocument.getPicturesTable().getAllPictures();
        out.println(pics);
        Iterator var8 = pics.iterator();

        //转换图片
        imgToBase64 imgToBase64pl=new imgToBase64();
        int i=1;
        while(var8.hasNext()) {
            Picture pic = (Picture)var8.next();
            //下载图片
           // pic.writeImageContent(new FileOutputStream(imagePathStr + pic.suggestFullFileName()));
            //System.out.println("第"+i++ +":"+imgToBase64pl.convertFileToBase64(imagePathStr + pic.suggestFullFileName()));
        }

        Document htmlDocument = wordToHtmlConverter.getDocument();
        //生成空文档
        DOMSource domSource = new DOMSource(htmlDocument);
        //获取路径下html
        StreamResult streamResult = new StreamResult(new File(targetFileName));
        //设置html文件规范
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty("encoding", "utf-8");
        serializer.setOutputProperty("indent", "yes");
        serializer.setOutputProperty("method", "html");
        serializer.transform(domSource, streamResult);
        out.println("doc转换完毕!"+streamResult.getSystemId());


    }

}

依赖:



    4.0.0
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    demo

    
        1.8
        UTF-8
        UTF-8
        2.3.7.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.projectlombok
            lombok
            1.16.10
        
        
        
            io.springfox
            springfox-swagger2
            2.9.2
        

        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            org.apache.poi
            poi
            3.14
        
        
            org.apache.poi
            poi-examples
            3.14
        
        
            org.apache.poi
            poi-excelant
            3.14
        
        
            org.apache.poi
            poi-scratchpad
            3.14
        
        
            org.apache.poi
            poi-ooxml
            3.14
        
        
            org.apache.poi
            ooxml-schemas
            1.3
        
        
            org.apache.pdfbox
            pdfbox
            2.0.9
        
        
            fr.opensagres.xdocreport
            xdocreport
            1.0.6
        
        
            org.apache.xmlbeans
            xmlbeans
            2.6.0
        

        
            org.projectlombok
            lombok
            1.16.10
        
    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                    UTF-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.3.7.RELEASE
                
                    com.lly.demo.DemoApplication
                
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
        
    


导航样式:

你可能感兴趣的:(html,java,前端)