SpringBoot Template模版适用(可用于转换为PDF,DOC,HTML等)

导入(不需要进行配置,会自动配置,):

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

	@Autowired
    private Configuration configuration;
    @Test
    public void test1() throws IOException, TemplateException {
		//Template template = configuration.getTemplate("settlementPayment.ftl");  默认获取resource/templates/ 下的ftl文件
        //字符串注入(后台可配置,灵活)
        String model = "你好 ${name}";
        Template template = new Template("xx1abvc", model, configuration); 
        //除了获取Template方式不同,其他都一样
        Map<String, String> map = Map.of("name", "李四");
        StringWriter writer = new StringWriter();
        template.process(map,writer);
        System.out.println(template.getName());
        System.out.println(writer.toString());
     }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Apache POI库来读取和解析DOC文档,然后使用Thymeleaf模板引擎将内容转换HTML。以下是一个简单的示例代码: 1. 首先,确保在你的项目中添加了Apache POI和Thymeleaf的依赖。 2. 创建一个Controller类,用于处理上传的DOC文件和转换HTML的操作: ```java import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.extractor.WordExtractor; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @Controller public class DocumentController { @PostMapping("/upload") public String uploadDocument(@RequestParam("file") MultipartFile file) throws IOException { // 保存上传的DOC文件 File docFile = convertMultipartFileToFile(file); // 读取DOC文件内容 HWPFDocument document = new HWPFDocument(FileCopyUtils.copyToByteArray(docFile)); WordExtractor extractor = new WordExtractor(document); String content = extractor.getText(); // 转换HTML String htmlContent = convertToHtml(content); // TODO: 在这里可以对HTML内容进行进一步处理,如保存到数据库或展示到页面 return "redirect:/"; } private File convertMultipartFileToFile(MultipartFile file) throws IOException { File convertedFile = new File(file.getOriginalFilename()); try (FileOutputStream fos = new FileOutputStream(convertedFile)) { fos.write(file.getBytes()); } return convertedFile; } private String convertToHtml(String content) { ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); resolver.setTemplateMode("HTML"); TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(resolver); Context context = new Context(); context.setVariable("content", content); return templateEngine.process("document-template", context); } } ``` 3. 创建一个名为`document-template.html`的Thymeleaf模板文件,用于DOC内容转换HTML。在模板中可以使用Thymeleaf的标签来渲染变量,如`[[${content}]]`。 ```html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div th:utext="${content}"></div> </body> </html> ``` 这样,当你上传一个DOC文件时,它将被转换HTML,并可以在浏览器中显示。你可以根据自己的需求进一步处理HTML内容,比如保存到数据库或展示到页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值