GridFS上传,查找,下载参考博文 学成:第4天3.3.3节
IOUtils工具类的使用
RestTemplate的详解:ResponseEntity继承了HttpEntity. 封装了返回的响应信息,包括 响应状态,响应头 和 响应体.
页面静态化的步骤
FreeMaker的静态化:模板+数据模型=输出,GridFS是MongoDB提供的用于持久化存储文件的模块。
GridFs存取文件的介绍:
存文件:当把一个文件存储到GridFS时,如果文件大于chunksize (每个chunk块大小为256KB),会先将文件按照chunk的大小分割成多个chunk块,最终将chunk块的信息存储在fs.chunks集合的多个文档中。然后将文件信息存储在fs.files集合的唯一一份文档中。其中fs.chunks集合中多个文档中的file_id字段对应fs.files集中文档”_id”字段。
取文件:读文件时,先根据查询条件在files集合中找到对应的文档,同时得到“_id”字段,再根据“_id”在chunks集合中查询所有“files_id”等于“_id”的文档。最后根据“n”字段顺序读取chunk的“data”字段数据,还原文件。
1.静态化程序远程请求DataUrl获取数据模型。
2. 静态化程序获取页面的模板信息
3. 执行页面静态化
1.获取数据模型
//获取页面模型数据信息
private Map getModelByPgaeId(String pageId){
CmsPage cmspage = this.getCmsById(pageId);
if(cmspage==null){
ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTSNAME);
}
String dataUrl = cmspage.getDataUrl();
if(StringUtils.isBlank(dataUrl)){
ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_DATAURLISNULL);
}
ResponseEntity<Map> forEntity = restTemplate.getForEntity(dataUrl, Map.class);
Map body = forEntity.getBody();
return body;
}
2.获取页面模板
其中GridFSBucket用于打开下载流对象
* @Author Mr.Wu
* @Date 2019/7/29 21:15
* @Version 1.0
**/
@Configuration
public class MongoConfig {
@Value("${spring.data.mongodb.database}")
String db;
@Bean
public GridFSBucket getGridFsBucket(MongoClient mongoClient){
MongoDatabase database = mongoClient.getDatabase(db);
GridFSBucket bucket = GridFSBuckets.create(database);
return bucket;
}
}
IOUtils中的toString():将输入流或数组中的内容转换为字符串。
从GridFS中取文件。
- 利用gridFsBucket打开一个下载流对象。
- 获取流
- 从流中取数据。
//获取页面模板
private String getTemplateByPageId(String pageId){
//取出页面的信息
CmsPage cmsPage = this.getCmsById(pageId);
if(cmsPage==null){
//页面不存在
ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTSNAME);
}
//获取页面的模板id
String templateId = cmsPage.getTemplateId();
if(StringUtils.isBlank(templateId)){
ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_TEMPLATEISNULL);
}
//查询模板信息
Optional<CmsTemplate> optional = this.cmsTemplateRepository.findById(templateId);
if(optional.isPresent()){
CmsTemplate cmsTemplate = optional.get();
//获取模板文件id
String templateFileId = cmsTemplate.getTemplateFileId();
//从GridFS中取模板文件内容
//根据文件id查询文件
GridFSFile gridFSFile = this.gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(templateFileId)));
//打开一个下载流对象
GridFSDownloadStream gridFSDownloadStream = this.gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
//创建GridFsResource对象,获取流
GridFsResource gridFsResource = new GridFsResource(gridFSFile, gridFSDownloadStream);
//从流中取数据
try {
String content = IOUtils.toString(gridFsResource.getInputStream(), "utf-8");
return content;
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
3.执行页面静态化
//执行静态化
private String generateHtml(String templateContent,Map model){
//创建配置对象
Configuration configuration = new Configuration(Configuration.getVersion());
//创建模板加载器
StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
//向configu配置模板加载器
configuration.setTemplateLoader(stringTemplateLoader);
//获取模板
try {
Template template = configuration.getTemplate("template");
//调用Api进行静态化
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
return content;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
总的集合
public String getPageHtml(String pageId){
//获取数据模型
Map model = this.getModelByPgaeId(pageId);
if(model==null){
ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_DATAISNULL);
}
//获取页面的模板信息
String template = this.getTemplateByPageId(pageId);
if(template==null){
ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_TEMPLATEISNULL);
}
//执行静态化
//执行静态化
String html = generateHtml(template, model);
return html;
}