| | |
| | | import freemarker.template.Template; |
| | | import freemarker.template.TemplateException; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import net.coobird.thumbnailator.Thumbnails; |
| | | import org.apache.poi.util.Units; |
| | | import org.apache.poi.xwpf.usermodel.*; |
| | | import org.springframework.mock.web.MockMultipartFile; |
| | |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.imageio.ImageIO; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.*; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | |
| | | Base64.Decoder decoder = Base64.getDecoder(); |
| | | 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 = 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) { |
| | | Thumbnails.of(in).scale(0.8).rotate(270).outputFormat("jpg").toOutputStream(out); |
| | | byte[] buffer = new byte[1024]; |
| | | int length; |
| | | // 从原始流读取数据并写入临时文件 |
| | |
| | | out.write(buffer, 0, length); |
| | | } |
| | | } |
| | | |
| | | |
| | | //处理图片 |
| | | for (XWPFParagraph paragraph : document.getParagraphs()) { |
| | | List<XWPFRun> runs = paragraph.getRuns(); |
| | |
| | | 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", |
| | | new FileInputStream(tempFile), XWPFDocument.PICTURE_TYPE_JPEG, |
| | | tempFile.getName(), |
| | | Units.toEMU(60), |
| | | Units.toEMU(30)); // 插入图片 |
| | | } |