|
@@ -1,214 +0,0 @@
|
|
|
-package com.thinkgem.jeesite.common.utils;
|
|
|
-
|
|
|
-import com.caimei.dfs.image.beens.ImageUploadInfo;
|
|
|
-import com.caimei.modules.archive.entity.CmProductArchiveContent;
|
|
|
-import com.caimei.modules.archive.utils.OssArchiveUtil;
|
|
|
-import com.caimei.modules.common.utils.UploadUtils;
|
|
|
-import com.caimei.modules.oss.utils.OSSUtils;
|
|
|
-import com.caimei.modules.sys.utils.UploadImageUtils;
|
|
|
-import com.thinkgem.jeesite.common.config.Global;
|
|
|
-import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
|
|
|
-import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
|
|
|
-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.Picture;
|
|
|
-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.xhtml.XHTMLConverter;
|
|
|
-//import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
|
|
|
-import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
-import org.jsoup.Jsoup;
|
|
|
-import org.w3c.dom.Document;
|
|
|
-
|
|
|
-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 java.io.*;
|
|
|
-import java.util.List;
|
|
|
-import java.util.UUID;
|
|
|
-
|
|
|
-public class HtmlGenerateUtils {
|
|
|
-
|
|
|
- private static final String htmlTempPath = Global.getConfig("archive.htmlTempPath");
|
|
|
-
|
|
|
- public static void main(String argv[]) {
|
|
|
- try {
|
|
|
-// doc2Html("C:\\Users\\Aslee\\Desktop\\测试html生成.doc","二手html生成");
|
|
|
-// docx2Html("C:\\Users\\Aslee\\Desktop\\docx上传.docx","二手html生成1");
|
|
|
-// clearTempFile("二手html生成");
|
|
|
-// docx2doc(new File("C:\\Users\\Aslee\\Desktop\\docx上传.docx"), "docx转doc");
|
|
|
- docx2pdf(new File("C:\\Users\\Aslee\\Desktop\\docx上传.docx"), "docx转pdf");
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * doc转换为html
|
|
|
- *
|
|
|
- * @param fileName
|
|
|
- * @param fileName
|
|
|
- * @throws TransformerException
|
|
|
- * @throws IOException
|
|
|
- * @throws ParserConfigurationException
|
|
|
- */
|
|
|
-// public static String doc2Html(String fileAllName,String fileName) throws TransformerException, IOException, ParserConfigurationException {
|
|
|
- public static String doc2Html(File file,String fileName) throws TransformerException, IOException, ParserConfigurationException {
|
|
|
- InputStream input = new FileInputStream(file);
|
|
|
- HWPFDocument wordDocument = new HWPFDocument(input);
|
|
|
-// HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileAllName));
|
|
|
- //图片文件夹
|
|
|
- File imageDirectory = new File(htmlTempPath + fileName + "-images");
|
|
|
- if (!imageDirectory.exists()) {
|
|
|
- imageDirectory.getParentFile().mkdirs();
|
|
|
- imageDirectory.mkdir();
|
|
|
- }
|
|
|
- // 保存图片
|
|
|
- List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();
|
|
|
- if (pics != null) {
|
|
|
- for (int i = 0; i < pics.size(); i++) {
|
|
|
- Picture pic = pics.get(i);
|
|
|
- try {
|
|
|
- String fileFullPath = htmlTempPath + fileName + "-images/" + fileName + "-" + pic.suggestFullFileName();
|
|
|
- pic.writeImageContent(new FileOutputStream(fileFullPath));
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
|
|
|
- wordToHtmlConverter.setPicturesManager((content, pictureType, suggestedName, widthInches, heightInches) -> {
|
|
|
- String imagePath = htmlTempPath + fileName + "-images\\" + fileName + "-" + suggestedName;
|
|
|
- File imageFile = new File(imagePath);
|
|
|
- String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
- String imageName = uuid + ".jpg";
|
|
|
- String imageUrl = OssArchiveUtil.ossUpload(imageName, "archiveFile/", imageFile, OSSUtils.getContentType(".jpg"), "inline");
|
|
|
- return imageUrl;
|
|
|
- });
|
|
|
- wordToHtmlConverter.processDocument(wordDocument);
|
|
|
-
|
|
|
-
|
|
|
- Document htmlDocument = wordToHtmlConverter.getDocument();
|
|
|
- ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
- DOMSource domSource = new DOMSource(htmlDocument);
|
|
|
- StreamResult streamResult = new StreamResult(out);
|
|
|
-
|
|
|
- TransformerFactory tf = TransformerFactory.newInstance();
|
|
|
- Transformer serializer = tf.newTransformer();
|
|
|
- serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
|
|
|
- serializer.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
|
- serializer.setOutputProperty(OutputKeys.METHOD, "html");
|
|
|
- serializer.transform(domSource, streamResult);
|
|
|
- out.close();
|
|
|
- writeFile(new String(out.toByteArray()), htmlTempPath + fileName + ".html");
|
|
|
-// System.out.println("Generate " + outPutFile + " with " + (System.currentTimeMillis() - startTime) + " ms.");
|
|
|
- return htmlTempPath + fileName + ".html";
|
|
|
- }
|
|
|
- /**
|
|
|
- * 写文件
|
|
|
- *
|
|
|
- * @param content
|
|
|
- * @param path
|
|
|
- */
|
|
|
- public static void writeFile(String content, String path) {
|
|
|
- FileOutputStream fos = null;
|
|
|
- BufferedWriter bw = null;
|
|
|
- try {
|
|
|
- File file = new File(path);
|
|
|
- fos = new FileOutputStream(file);
|
|
|
- bw = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));
|
|
|
- bw.write(content);
|
|
|
- } catch (FileNotFoundException fnfe) {
|
|
|
- fnfe.printStackTrace();
|
|
|
- } catch (IOException ioe) {
|
|
|
- ioe.printStackTrace();
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- if (bw != null) {
|
|
|
- bw.close();
|
|
|
- }
|
|
|
- if (fos != null) {
|
|
|
- fos.close();
|
|
|
- }
|
|
|
- } catch (IOException ie) {
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * docx格式word转换为html
|
|
|
- *
|
|
|
- * @param fileName docx文件路径
|
|
|
-// * @param outPutFile html输出文件路径
|
|
|
- * @throws TransformerException
|
|
|
- * @throws IOException
|
|
|
- * @throws ParserConfigurationException
|
|
|
- */
|
|
|
-// public static String docx2Html(String fileAllName, String fileName) throws IOException {
|
|
|
- /*public static String docx2Html(File file, String fileName) throws IOException {
|
|
|
- InputStream input = new FileInputStream(file);
|
|
|
- XWPFDocument document = new XWPFDocument(input);
|
|
|
-// XWPFDocument document = new XWPFDocument(new FileInputStream(fileAllName));
|
|
|
- String fileOutName = htmlTempPath + fileName +".html";
|
|
|
- XHTMLOptions options = XHTMLOptions.create().indent(4);
|
|
|
- // 导出图片
|
|
|
- File imageFolder = new File(htmlTempPath+fileName+"-images/");
|
|
|
- options.setExtractor(new FileImageExtractor(imageFolder));
|
|
|
- // URI resolver word的html中图片的目录路径
|
|
|
- options.URIResolver(new BasicURIResolver(htmlTempPath));
|
|
|
- File outFile = new File(fileOutName);
|
|
|
- outFile.getParentFile().mkdirs();
|
|
|
- OutputStream out = new FileOutputStream(outFile);
|
|
|
- XHTMLConverter.getInstance().convert(document, out, options);
|
|
|
-// System.out.println("Generate " + fileOutName + " with " + (System.currentTimeMillis() - startTime) + " ms.");
|
|
|
- return htmlTempPath + fileName + ".html";
|
|
|
- }*/
|
|
|
-
|
|
|
- public static void clearTempFile(String fileName){
|
|
|
- //清除html文件
|
|
|
- File file = new File(htmlTempPath + fileName + ".html");
|
|
|
- file.delete();
|
|
|
- //清除图片
|
|
|
- File imageFolder = new File(htmlTempPath + fileName + "-images");
|
|
|
- deleteDir(imageFolder);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private static boolean deleteDir(File dir) {
|
|
|
- if (dir.isDirectory()) {
|
|
|
- String[] children = dir.list();
|
|
|
- //递归删除目录中的子目录下
|
|
|
- for (int i=0; i<children.length; i++) {
|
|
|
- boolean success = deleteDir(new File(dir, children[i]));
|
|
|
- if (!success) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- // 目录此时为空,可以删除
|
|
|
- return dir.delete();
|
|
|
- }
|
|
|
-
|
|
|
- public static File docx2pdf(File file, String fileName) throws IOException {
|
|
|
- InputStream input = new FileInputStream(file);
|
|
|
- XWPFDocument xwpfDocument = new XWPFDocument(input);
|
|
|
- PdfOptions pdfOptions = PdfOptions.create();
|
|
|
- OutputStream out = new FileOutputStream(new File(htmlTempPath + fileName + ".pdf"));
|
|
|
- PdfConverter.getInstance().convert(xwpfDocument, out, pdfOptions);
|
|
|
- return null;
|
|
|
- }/*public static File docx2doc(File file, String fileName) throws IOException {
|
|
|
- InputStream input = new FileInputStream(file);
|
|
|
- XWPFDocument xwpfDocument = new XWPFDocument(input);
|
|
|
-
|
|
|
- WordToHtmlConverter
|
|
|
- }*/
|
|
|
-
|
|
|
-}
|