| | |
| | | package com.ruoyi.web.controller.tool; |
| | | |
| | | import cn.hutool.core.io.FileUtil; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import freemarker.cache.ClassTemplateLoader; |
| | | import freemarker.template.Configuration; |
| | | import freemarker.template.Template; |
| | |
| | | import javax.imageio.ImageIO; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.*; |
| | | import java.nio.file.Files; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | |
| | | XWPFDocument document = new XWPFDocument(fis); |
| | | XWPFParagraph pic = document.createParagraph(); |
| | | Base64.Decoder decoder = Base64.getDecoder(); |
| | | byte[] imageByte = decoder.decode(url); |
| | | InputStream stream = new ByteArrayInputStream(imageByte); |
| | | if(StringUtils.isNotEmpty(url)){ |
| | | byte[] imageByte = decoder.decode(url); |
| | | InputStream stream = new ByteArrayInputStream(imageByte); |
| | | // File tempFile = FileUtil.createTempFile("/usr/local/project/file/temp", ".jpg", true); |
| | | File tempFile = File.createTempFile("/usr/local/project/file/temp", ".jpg"); |
| | | tempFile.deleteOnExit(); // 程序结束时删除文件 |
| | | File tempFile = File.createTempFile("/usr/local/project/file/temp", ".jpg"); |
| | | tempFile.deleteOnExit(); // 程序结束时删除文件 |
| | | |
| | | try (OutputStream out = new FileOutputStream(tempFile); |
| | | InputStream in = stream) { |
| | | Thumbnails.of(in).scale(0.8).rotate(270).outputFormat("jpg").toOutputStream(out); |
| | | byte[] buffer = new byte[1024]; |
| | | int length; |
| | | // 从原始流读取数据并写入临时文件 |
| | | while ((length = in.read(buffer)) > 0) { |
| | | out.write(buffer, 0, length); |
| | | try (OutputStream out = Files.newOutputStream(tempFile.toPath()); |
| | | InputStream in = stream) { |
| | | Thumbnails.of(in).scale(0.8).rotate(270).outputFormat("jpg").toOutputStream(out); |
| | | byte[] buffer = new byte[1024]; |
| | | int length; |
| | | // 从原始流读取数据并写入临时文件 |
| | | while ((length = in.read(buffer)) > 0) { |
| | | 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(tempFile), XWPFDocument.PICTURE_TYPE_JPEG, |
| | | tempFile.getName(), |
| | | Units.toEMU(60), |
| | | Units.toEMU(30)); // 插入图片 |
| | | //处理图片 |
| | | 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, |
| | | tempFile.getName(), |
| | | Units.toEMU(60), |
| | | Units.toEMU(30)); // 插入图片 |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | // 处理段落 |
| | | for (XWPFParagraph paragraph : document.getParagraphs()) { |
| | | replaceParagraph(paragraph, dataMap); |