java在线word预览
这个是一个比较简单的word转换成html来进行在线预览
封成了一个util,代码如下
public class WordToHtmlUtils { /** * @param wordPath 要转换文件的绝对路径 * @param htmlName 生成的html文件名 * @param relativelyPath 存放的相对路径 /insfile/wordtohtml/ * @param aimsFilePath 存放的文件名 wordtohtml * @return * @throws IOException */ public static String Word2007ToHtml(String wordPath, String htmlName, String relativelyPath, String aimsFilePath) throws IOException { //静态资源路径,用来存放生成的的html和图片的 String filepath = App.FILE_SYS+File.separatorChar+aimsFilePath+File.separatorChar ; File f = new File(wordPath); if (!f.exists()) { System.out.println("Sorry File does not Exists!"); } else { if (f.getName().endsWith(".docx") || f.getName().endsWith(".DOCX")) { // 1) 加载word文档生成 XWPFDocument对象 InputStream in = new FileInputStream(f); XWPFDocument document = new XWPFDocument(in); // 2) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录) File imageFolderFile = new File(filepath); XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile)); options.setExtractor(new FileImageExtractor(imageFolderFile)); options.setIgnoreStylesIfUnused(false); options.setFragment(true); //这里可以替换生成的图片为相对路径 options.URIResolver(new IURIResolver() { //这个uri就是图片在aimsFilePath下的相对路径
//如果在线预览没有显示图片,在这里看看 @Override public String resolve(String uri) { return relativelyPath + uri;//返回图片url } }); // 3) 将 XWPFDocument转换成XHTML OutputStream out = new FileOutputStream(new File(filepath + htmlName)); XHTMLConverter.getInstance().convert(document, out, options); //也可以使用字符数组流获取解析的内容 // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // XHTMLConverter.getInstance().convert(document, baos, options); // String content = baos.toString(); // System.out.println(content); // baos.close(); } else { System.out.println("Enter only MS Office 2007+ files"); } } return relativelyPath+htmlName; } /** * @param wordPath 要转换文件的绝对路径 * @param htmlName 生成的html文件名 * @param relativelyPath 存放的相对路径 /insfile/wordtohtml/ * @param aimsFilePath 存放的文件名 wordtohtml * @param imageFile 存放图片的目录 doc * @return * @throws IOException * @throws TransformerException * @throws ParserConfigurationException */ public static String Word2003ToHtml(String wordPath, String htmlName, String relativelyPath, String aimsFilePath, String imageFile) throws IOException, TransformerException, ParserConfigurationException { final String imagepath = App.FILE_SYS+File.separator+aimsFilePath+File.separator+imageFile+File.separator;//解析时候如果doc文件中有图片 图片会保存在此路径 String filepath = App.FILE_SYS+File.separatorChar+aimsFilePath+File.separatorChar ; final String file =wordPath; 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 relativelyPath+File.separator+imageFile+File.separator+ suggestedName; } }); //解析word文档 wordToHtmlConverter.processDocument(wordDocument); Document htmlDocument = wordToHtmlConverter.getDocument(); File htmlFile = new File(filepath + htmlName); OutputStream outStream = new FileOutputStream(htmlFile); //也可以使用字符数组流获取解析的内容 // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // OutputStream outStream = new BufferedOutputStream(baos); 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); //也可以使用字符数组流获取解析的内容 // String content = baos.toString(); // System.out.println(content); // baos.close(); outStream.close(); return relativelyPath+File.separator+htmlName; } }
//图片存放路径根据自己需求来修改,支持doc, docx