| | |
| | | package com.ruoyi.web.controller.tool; |
| | | |
| | | import cn.hutool.core.io.FileUtil; |
| | | import freemarker.cache.ClassTemplateLoader; |
| | | import freemarker.template.Configuration; |
| | | import freemarker.template.Template; |
| | |
| | | Base64.Decoder decoder = Base64.getDecoder(); |
| | | byte[] imageByte = decoder.decode(url); |
| | | InputStream stream = new ByteArrayInputStream(imageByte); |
| | | File tempFile = File.createTempFile("/usr/local/project/file/temp", ".jpg"); |
| | | tempFile.deleteOnExit(); // 程序结束时删除文件 |
| | | File tempFile = FileUtil.createTempFile("/usr/local/project/file/temp", ".jpg", true); |
| | | // File tempFile = File.createTempFile("/usr/local/project/file/temp", ".jpg"); |
| | | // tempFile.deleteOnExit(); // 程序结束时删除文件 |
| | | |
| | | try (OutputStream out = new FileOutputStream(tempFile); |
| | | InputStream in = stream) { |
| | |
| | | out.write(buffer, 0, length); |
| | | } |
| | | } |
| | | |
| | | //处理图片 |
| | | for (XWPFParagraph paragraph : document.getParagraphs()) { |
| | | List<XWPFRun> runs = paragraph.getRuns(); |
| | | for (XWPFRun run : runs) { |
| | | String text = run.getText(0); |
| | | if (text != null && text.contains("picture")) { |
| | | run.setText("", 0); // 清除占位符文本 |
| | | run.addPicture( |
| | | new FileInputStream(new File("/usr/local/project/file/sign.jpg")), XWPFDocument.PICTURE_TYPE_JPEG, |
| | | "/usr/local/project/file/sign.jpg", |
| | | Units.toEMU(60), |
| | | Units.toEMU(30)); // 插入图片 |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 处理段落 |
| | | for (XWPFParagraph paragraph : document.getParagraphs()) { |
| | | replaceParagraph(paragraph, dataMap); |
| | |
| | | for (XWPFParagraph paragraph : cell.getParagraphs()) { |
| | | replaceParagraph(paragraph, dataMap); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | //处理图片 |
| | | for (XWPFParagraph paragraph : document.getParagraphs()) { |
| | | List<XWPFRun> runs = paragraph.getRuns(); |
| | | for (XWPFRun run : runs) { |
| | | String text = run.getText(0); |
| | | if (text != null && text.contains("picture")) { |
| | | run.setText("", 0); // 清除占位符文本 |
| | | run.addPicture( |
| | | new FileInputStream(tempFile), XWPFDocument.PICTURE_TYPE_JPEG, |
| | | "/usr/local/project/file/sign.jpg", |
| | | Units.toEMU(60), |
| | | Units.toEMU(30)); // 插入图片 |
| | | } |
| | | } |
| | | } |