| | |
| | | import cn.hutool.core.lang.UUID; |
| | | import com.aspose.words.Document; |
| | | import com.aspose.words.SaveFormat; |
| | | import com.deepoove.poi.XWPFTemplate; |
| | | import com.deepoove.poi.xwpf.NiceXWPFDocument; |
| | | import com.google.common.collect.Maps; |
| | | import com.product.common.lang.StringUtils; |
| | | import com.product.core.config.Global; |
| | |
| | | import java.io.*; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Properties; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | |
| | | public class FileUtil { |
| | | |
| | | private FileUtil() { |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | System.out.println(FileUtil.checkDocType(new File("D:\\downloads\\关于协调解决西宁万达地产置业有限公司商品房预售资金监管账户冻结相关问题 (1).doc"))); |
| | | } |
| | | |
| | | public static String checkDocType(File file) { |
| | |
| | | * @return |
| | | */ |
| | | public static File mergeFile(File source, File target) throws Exception { |
| | | if ("doc".equals(FileUtil.checkDocType(source))) { |
| | | source = toDocx(source); |
| | | } |
| | | boolean delTarget = false; |
| | | if ("doc".equals(FileUtil.checkDocType(target))) { |
| | | target = toDocx(target); |
| | | delTarget = true; |
| | | } |
| | | XWPFTemplate template = XWPFTemplate.compile(source); |
| | | NiceXWPFDocument xwpfDocument = template.getXWPFDocument(); |
| | | try { |
| | | NiceXWPFDocument sub = new NiceXWPFDocument(new FileInputStream((target))); |
| | | NiceXWPFDocument newDoc = xwpfDocument.merge(sub); |
| | | String tempFile = Global.getSystemConfig("temp.dir", "./attachment/temp") + File.separator + UUID.randomUUID() + ".docx"; |
| | | try (FileOutputStream out = new FileOutputStream(tempFile)) { |
| | | newDoc.write(out); |
| | | newDoc.close(); |
| | | return new File(tempFile); |
| | | } catch (IOException e) { |
| | | throw e; |
| | | } finally { |
| | | if (delTarget) { |
| | | target.delete(); |
| | | } |
| | | newDoc.close(); |
| | | sub.close(); |
| | | } |
| | | } catch (IOException e) { |
| | | throw e; |
| | | } catch (Exception e) { |
| | | throw e; |
| | | } |
| | | return MergeWordDocuments.mergeFile(source, target); |
| | | } |
| | | |
| | | public static File toDocx(File file) { |