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; }
转载于:https://www.cnblogs.com/whb11/p/6273531.html
相关资源:poi word2003转html