获取后台数据并拼写HTML模板

模板下载地址:http://download.csdn.net/download/tikaber/10154297

/**
	 * 
	 * html模板
	 * 
	 * */
	public Map<String, Object> templateHtml(Info info) {
		Map<String, Object> uploadURL = new HashMap<>();
		String fileame ="";
		try {
			String title = info.getTitle();
			String comefrom = info.getComefrom();
			String content = info.getContent();
			// 模板路径
			String filePath = Thread.currentThread().getContextClassLoader().getResource("templates/info_template.html").getPath();
//			String pathBack = Thread.currentThread().getContextClassLoader().getResource("templates").getPath();
			log.info(filePath);
			String templateContent = "";
			// 读取模板文件
			FileInputStream fileinputstream = new FileInputStream(filePath);
			int lenght = fileinputstream.available();
			byte bytes[] = new byte[lenght];
			fileinputstream.read(bytes);
			fileinputstream.close();
			templateContent = new String(bytes);
			log.info(templateContent);
			// 替换掉模板中相应的地方
			templateContent = templateContent.replaceAll("###title###", title);
			templateContent = templateContent.replaceAll("###comefrom###", comefrom);
			templateContent = templateContent.replaceAll("###content###", content);
			log.info(templateContent);
			// 根据时间得文件名
			Calendar calendar = Calendar.getInstance();
			fileame = String.valueOf(calendar.getTimeInMillis()) + ".html";
			// 生成的html文件保存路径。
			fileame = "/" + fileame;
			// 建立文件输出流
			FileOutputStream fileoutputstream = new FileOutputStream(fileame);
			log.info("文件输出路径:");
			log.info(fileame);
			byte tag_bytes[] = templateContent.getBytes();
			fileoutputstream.write(tag_bytes);
			fileoutputstream.close();
		} catch (Exception e) {
			log.info(e.toString());
		}
		if (!StringUtils.isEmpty(fileame)) {
			uploadURL.put("url", fileame);
		}
		return uploadURL;
	}


猜你喜欢

转载自blog.csdn.net/tikaber/article/details/78775149