首页 > 其他分享 >利用freemarker和minIO生成文章详情html并存入minIO

利用freemarker和minIO生成文章详情html并存入minIO

时间:2023-06-28 14:44:43浏览次数:36  
标签:heima minIO freemarker apArticleContent html article import com

package com.heima.article.test;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.ArticleApplication;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.service.ApArticleService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest(classes = ArticleApplication.class)
@RunWith(SpringRunner.class)
public class ArticleFreemarkerTest {

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private ApArticleService articleService;



    @Test
    public void createStaticUrlTest() throws IOException, TemplateException {
        //已知文章id
        //1、获取文章内容
        List<ApArticleContent> apArticleContents = apArticleContentMapper.selectList(Wrappers.<ApArticleContent>lambdaQuery().select(ApArticleContent::getArticleId, ApArticleContent::getContent));
        for(ApArticleContent apArticleContent: apArticleContents){

            if(apArticleContent != null && StringUtils.isNotBlank(apArticleContent.getContent())){
                //2、文章内容通过freemarker生成html文件
                Template template = configuration.getTemplate("article.ftl");
                //数据模型
                Map<String, Object> content = new HashMap<>();
                content.put("content", JSONArray.parseArray(apArticleContent.getContent()));
                StringWriter out = new StringWriter();
                //合成
                template.process(content, out);
                //3、把html文件上传到minio中
                ByteArrayInputStream in = new ByteArrayInputStream(out.toString().getBytes());
                String path = fileStorageService.uploadHtmlFile("", apArticleContent.getArticleId() + ".html", in);
                //4、修改ap_article表,保存static_url字段
                articleService.update(Wrappers.<ApArticle>lambdaUpdate().eq(ApArticle::getId ,apArticleContent.getArticleId())
                        .set(ApArticle::getStaticUrl, path));
            }
        }


    }
}

 

标签:heima,minIO,freemarker,apArticleContent,html,article,import,com
From: https://www.cnblogs.com/fxzm/p/17511347.html

相关文章

  • (转)基于velero+minio对k8s进行备份和恢复
    原文:https://www.cnblogs.com/cyh00001/p/16548774.html一、velero介绍Velero是由vmware公司团队研发开源工具,用于安全地备份、恢复和迁移Kubernetes集群和持久卷。它可以在本地和公共云中运行。Velero由一个在您的Kubernetes集群中作为部署运行的服务器进程和一个命令行......
  • HTML页面中返回顶部的几种实现方式
    第一种:引用外部jQuery新建HTML页面,将下面代码复制保存,通过浏览器打开,即可看到效果.<!doctypehtml><htmllang="en"><head><metacharset="UTF-8"><title>doc</title><style>.arrow{border:9pxsolidtransparent;......
  • Qt QString 转换 Html 实体
    开发环境:Qt5.12.2+QtCreator1、问题背景:读取日志在QTextBrowser显示中使用HTML设置显示颜色,如果日志内容中存在HTML相关关键字符,会导致显示异常2、问题显示:ui->textBrowser_LogText->append("<fontcolor=\"#666666\">"+log+"</font>");数据遇......
  • FreeMarker HelloWorld 一个例子
    FreeMarker是开源的模板框架主页:http://www.freemarker.org/ 现在我们就开始我们的FreeMarker版的HelloWorld之旅吧。1.下载最新的freemarkerjar包FreeMarkerhttp://cznic.dl.sourceforge.net/project/freemarker/freemarker/2.3.20/freemarker-gae-2.3.20.jar2.解压你下载的......
  • HTML5中如何上传Resize后的图片
    参考资料:不依赖form标签,而是自己定义FormData上传数据,文件被编码为一个Blob或File对象:https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects如何用jquery上传FormData:http://stackoverflow.com/questions/9622901/how-to-upload-a-file-using-jquery-ajax-a......
  • MinIO——分布式文件系统
    对象如图片、视频、文档等存储方式对比:优点缺点服务器磁盘:开发便捷、成本低扩展困难分布式文件系统:容易实现扩容复杂度高    第三方存储:开发简单,功能强大,免维护......
  • JDK/bin目录下的不同exe文件的用途(appletviewer、HtmlConverter、jar、java、javac、
    目录---------------------------------------1.javacexe2.appletviewerexe3.jarexe4.javadocexe5.javahexe6.HtmlConverterexe7.orbdexe8.policytoolexe9.rmicexe10.rmidexe11.rmiregistryexe12.serialverexe13.servertoolexe14.rmic15.rmid16.rmiregistry17.serialver18.jarsi......
  • html编辑器 复制word里面带图文的文章,图片可以直接显示
    ​ 在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper。通过知乎提供的思路找到粘贴的原理,通过TheViper找到粘贴图片的方法。其原理为一下步骤:监听粘贴事件;【用于插入图片】获取光标位置;【......
  • Python -Flask HTML <img 显示本地图片失败,怎么破?
    大家好,我是皮皮。一、前言前几天在Python白银群【膨】问了一个Flask图片显示的问题,这里拿出来给大家分享下。运行之后图片加载不出来。二、实现过程这里【此类生物】给了一个思路,flask运行当前路径是主程序路径,把图片路径改成绝对路径。结果页面还是加载不出来。后来以......
  • HTML Over the wire 框架 Unpoly 和 React 的使用场合比较
    Unpoly是一个轻量级的JavaScript框架,它允许您通过HTMLoverthewire的方式进行Web应用程序开发。HTMLoverthewire是一种网络通信模式,它将由服务器生成的HTML片段作为响应发送给客户端,而不是传统的JSON数据。这样,服务器端可以处理更多逻辑,而客户端可以专注于渲染和......