zoukankan      html  css  js  c++  java
  • 2003版本word转换成html

    public static String Word2003ToHtml(HttpServletRequest request) throws IOException, TransformerException, ParserConfigurationException {
    String files = "/statics/mailfile";
    String images = "/statics/h-ui/images";
    String filepath = request.getSession().getServletContext().getRealPath(files)+"\";
    final String imagepath = request.getSession().getServletContext().getRealPath(images)+"\";
    String fileName = "Camelot_CV.doc";
    String htmlName = generateString()+".html";
    final String file = filepath + fileName;
    InputStream input = new FileInputStream(new File(file));
    HWPFDocument wordDocument = new HWPFDocument(input);
    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) {
    File imgPath = new File(imagepath);
    if(!imgPath.exists()){//图片目录不存在则创建
    imgPath.mkdirs();
    }
    File file = new File(imagepath + suggestedName);
    try {
    OutputStream os = new FileOutputStream(file);
    os.write(content);
    os.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return imagepath + suggestedName;
    }
    });

    //解析word文档
    wordToHtmlConverter.processDocument(wordDocument);
    Document htmlDocument = wordToHtmlConverter.getDocument();

    String htmlFileName = filepath + htmlName;
    File htmlFile = new File(htmlFileName);
    OutputStream outStream = new FileOutputStream(htmlFile);

    DOMSource domSource = new DOMSource(htmlDocument);
    StreamResult streamResult = new StreamResult(outStream);

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer serializer = factory.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.METHOD, "html");

    serializer.transform(domSource, streamResult);
    outStream.close();
    /* try {
    writeWordFile(request,htmlFileName,new ResStandardResumeDTO());
    } catch (Exception e) {
    e.printStackTrace();
    }*/
    return htmlFileName;
    }

  • 相关阅读:
    浅谈对java中锁的理解
    Spring 4 支持的 Java 8 特性
    【转】Java线程面试题 Top 50
    JVM知识点总览-中高级Java工程师面试必备
    [LeetCode] 195. Tenth Line 第十行
    [LeetCode] 281. Zigzag Iterator 之字形迭代器
    [LeetCode] 324. Wiggle Sort II 摆动排序 II
    [LeetCode] 280. Wiggle Sort 摆动排序
    [LeetCode] 167. Fraction to Recurring Decimal 分数转循环小数
    [LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列
  • 原文地址:https://www.cnblogs.com/whb11/p/6273531.html
Copyright © 2011-2022 走看看