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;
    }

  • 相关阅读:
    Shiro SessionContext和SessionKey的设计概念
    Shiro DefaultWebSessionManager的设计概念
    Shiro SessionDAO的设计概念
    Shiro DefaultSessionManager的设计概念
    Shiro 关于校验Session过期、有效性的设计概念
    Shiro AbstractValidatingSessionManager设计概念
    Windows 查看端口及杀掉进程
    Shiro Session及SessionManager的设计概念
    jQuery操作DOM节点
    jQuery动画效果
  • 原文地址:https://www.cnblogs.com/whb11/p/6273531.html
Copyright © 2011-2022 走看看