Java如何实现Word/Pdf/TXT转html
这篇文章给大家分享的是有关Java如何实现Word/Pdf/TXT转html的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
成都创新互联公司是一家专业提供平和企业网站建设,专注与成都网站制作、成都网站设计、H5场景定制、小程序制作等业务。10年已为平和众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
一:Java实现将word转换为html
1:引入依赖
fr.opensagres.xdocreport fr.opensagres.xdocreport.document 1.0.5 fr.opensagres.xdocreport org.apache.poi.xwpf.converter.xhtml 1.0.5 org.apache.poi poi 3.12 org.apache.poi poi-scratchpad 3.12
2:代码demo
package com.svse.controller; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.PicturesManager; import org.apache.poi.hwpf.converter.WordToHtmlConverter; import org.apache.poi.hwpf.usermodel.PictureType; import org.apache.poi.xwpf.converter.core.BasicURIResolver; import org.apache.poi.xwpf.converter.core.FileImageExtractor; import org.apache.poi.xwpf.converter.core.FileURIResolver; import org.apache.poi.xwpf.converter.core.IURIResolver; import org.apache.poi.xwpf.converter.core.IXWPFConverter; import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter; import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; /** * word 转换成html */ public class TestWordToHtml { public static final String STORAGEPATH="C://works//files//"; public static final String IP="192.168.30.222"; public static final String PORT="8010"; public static void main(String[] args) throws IOException, TransformerException, ParserConfigurationException { TestWordToHtml wt=new TestWordToHtml(); //wt.Word2003ToHtml("甲骨文考证.doc"); wt.Word2007ToHtml("甲骨文考证.docx"); } /** * 2003版本word转换成html * @throws IOException * @throws TransformerException * @throws ParserConfigurationException */ public void Word2003ToHtml(String fileName) throws IOException, TransformerException, ParserConfigurationException { final String imagepath = STORAGEPATH+"fileImage/";//解析时候如果doc文件中有图片 图片会保存在此路径 final String strRanString=getRandomNum(); String filepath =STORAGEPATH; String htmlName =fileName.substring(0, fileName.indexOf("."))+ "2003.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 +strRanString+suggestedName); try { OutputStream os = new FileOutputStream(file); os.write(content); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "http://"+IP+":"+PORT+"//uploadFile/fileImage/"+strRanString+suggestedName; // return imagepath +strRanString+suggestedName; } }); //解析word文档 wordToHtmlConverter.processDocument(wordDocument); Document htmlDocument = wordToHtmlConverter.getDocument(); File htmlFile = new File(filepath +strRanString+htmlName); 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(); System.out.println("生成html文件路径:"+ "http://"+IP+":"+PORT+"//uploadFile/"+strRanString+htmlName); } /** * 2007版本word转换成html * @throws IOException */ public void Word2007ToHtml(String fileName) throws IOException { final String strRanString=getRandomNum(); String filepath = STORAGEPATH+strRanString; String htmlName =fileName.substring(0, fileName.indexOf("."))+ "2007.html"; File f = new File(STORAGEPATH+fileName); if (!f.exists()) { System.out.println("Sorry File does not Exists!"); } else { if (f.getName().endsWith(".docx") || f.getName().endsWith(".DOCX")) { try { // 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.URIResolver(new IURIResolver() { public String resolve(String uri) { //http://192.168.30.222:8010//uploadFile/.... return "http://"+IP+":"+PORT+"//uploadFile/"+strRanString +"/"+ uri; } }); options.setIgnoreStylesIfUnused(false); options.setFragment(true); // 3) 将 XWPFDocument转换成XHTML OutputStream out = new FileOutputStream(new File(filepath + htmlName)); IXWPFConverterconverter = XHTMLConverter.getInstance(); converter.convert(document,out, options); //XHTMLConverter.getInstance().convert(document, out, options); System.out.println("html路径:"+"http://"+IP+":"+PORT+"//uploadFile/"+strRanString+htmlName); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("Enter only MS Office 2007+ files"); } } } /** *功能说明:生成时间戳 *创建人:zsq *创建时间:2019年12月7日 下午2:37:09 * */ public static String getRandomNum(){ Date dt = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String str=sdf.format(dt); return str; } }
二:Java实现将Pdf转换为html
1: 引入依赖
net.sf.cssbox pdf2dom 1.7 org.apache.pdfbox pdfbox 2.0.12 org.apache.pdfbox pdfbox-tools 2.0.12
2:代码Demo
public class PdfToHtml { /* pdf转换html */ public void pdfToHtmlTest(String inPdfPath,String outputHtmlPath) { // String outputPath = "C:\\works\\files\\ZSQ保密知识测试题库.html"; //try() 写在()里面会自动关闭流 try{ BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outputHtmlPath)),"utf-8")); //加载PDF文档 //PDDocument document = PDDocument.load(bytes); PDDocument document = PDDocument.load(new File(inPdfPath)); PDFDomTree pdfDomTree = new PDFDomTree(); pdfDomTree.writeText(document,out); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { PdfToHtml ph=new PdfToHtml(); String pdfPath="C:\\works\\files\\武研中心行政考勤制度.pdf"; String outputPath="C:\\works\\files\\武研中心行政考勤制度.html"; ph.pdfToHtmlTest(pdfPath,outputPath); } }
三:Java实现将TXT转换为html
/* * txt文档转html filePath:txt原文件路径 htmlPosition:转化后生成的html路径 */ public static void txtToHtml(String filePath, String htmlPosition) { try { //String encoding = "GBK"; File file = new File(filePath); if (file.isFile() && file.exists()) { // 判断文件是否存在 InputStreamReader read = new InputStreamReader(new FileInputStream(file), "GBK"); // 考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); // 写文件 FileOutputStream fos = new FileOutputStream(new File(htmlPosition)); OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK"); BufferedWriter bw = new BufferedWriter(osw); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { bw.write(" "+lineTxt + ""); } bw.close(); osw.close(); fos.close(); read.close(); } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } }
感谢各位的阅读!关于“Java如何实现Word/Pdf/TXT转html”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
名称栏目:Java如何实现Word/Pdf/TXT转html
文章位置:http://azwzsj.com/article/jciihh.html