// content 内容
// path 文件路径
public static boolean addHtml(String content, String path){
//用于存储html字符串
StringBuilder stringHtml = new StringBuilder();
//输入HTML文件内容
stringHtml.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
stringHtml.append("<head>");
//stringHtml.append("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
stringHtml.append("<title>标题</title>");
stringHtml.append("</head>");
stringHtml.append("<body>");
stringHtml.append(content);
stringHtml.append("</body></html>");
try{
File f = new File(path);
if (f.getParentFile() != null || !f.getParentFile().isDirectory()) {
// 创建文件
f.getParentFile().mkdirs();
}
//将HTML文件内容写入文件中
FileOutputStream fileOutputStream=new FileOutputStream(path);
PrintStream printStream=new PrintStream(fileOutputStream);
// 转码,转化为utf-8
String htmls = new String(stringHtml.toString().getBytes("utf-8"),"utf-8");
printStream.println(htmls);
fileOutputStream.flush();
printStream.flush();
fileOutputStream.close();
printStream.close();
return true;
}catch (Exception e) {
e.printStackTrace();
return false;
}
}
java 动态生成html文件
最新推荐文章于 2023-11-29 01:00:00 发布