JAVA读取模板,向Linux服务器写入html文件
来源:互联网 发布:深圳美工师到哪考 编辑:程序博客网 时间:2024/06/15 17:27
需求:对不同岗位的员工进行线上考核,然后生成考核文件(以html文件形式保存在服务器)
1>一共6个岗位,写好了6个模板,在浏览器预览效果如下(截取一部分):
------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------
#*****# 表示在生成html文件时需要替换成考核分数的部分
2>我把这6个模板上传到了Linux上面,生成html文件时需要从服务器读取这些模板:
3>java代码:根据不同岗位标识返回不同的ModelAndView
在考核页面考核完成后点击提交,通过ajax()把考核分数提交到后台
function ajaxUpload(){ var options = {url : "<m:url value='/empCheck/checkproducehtml.do'/>",type : "post",dataType:"text",success :function(d) {window.parent.closeCheck();}};$('#form').ajaxSubmit(options); }
进入方法---->请求参数绑定在checkHtmlForm实体中,包括员工姓名,考核月份,每一项考核分数和总分
根据不同的考核标识读取服务器不同的html模板,模板中需要替换的部分例如:
生成html文件代码如下:
先读取模板---进行字符串替换---写入文件
try { System.setProperty("sun.jnu.encoding","UTF-8"); final String encoding = System.getProperty("file.encoding"); System.out.println("encoding-----------------------:"+encoding); String templateContent = ""; BufferedReader r=new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"UTF-8")); String str=""; while((str = r.readLine())!=null) { templateContent += str; } templateContent = templateContent.replaceAll("#name#", checkHtmlForm.getEmpName()); templateContent = templateContent.replaceAll("#month#", checkHtmlForm.getCheckMonth()); templateContent = templateContent.replaceAll("#core1#", checkHtmlForm.getCore1()); templateContent = templateContent.replaceAll("#core2#", checkHtmlForm.getCore2()); templateContent = templateContent.replaceAll("#core3#", checkHtmlForm.getCore3()); templateContent = templateContent.replaceAll("#core4#", checkHtmlForm.getCore4()); templateContent = templateContent.replaceAll("#core5#", checkHtmlForm.getCore5()); templateContent = templateContent.replaceAll("#total#", checkHtmlForm.getTotalcore()); if(checkHtmlForm.getCore6()!=null){ templateContent = templateContent.replaceAll("#core6#", checkHtmlForm.getCore6()); } if(checkHtmlForm.getCore7()!=null){ templateContent = templateContent.replaceAll("#core7#", checkHtmlForm.getCore7()); } if(checkHtmlForm.getCore8()!=null){ templateContent = templateContent.replaceAll("#core8#", checkHtmlForm.getCore8()); } logger.debug("文件内容=====" + templateContent); String fileame = checkHtmlForm.getEmpNo() + "-" + checkHtmlForm.getCheckMonth() + ".html";//文件名 //System.out.println(checkHtmlForm.getEmpName()); fileame = "/usr/dnfile/checkfile/" + fileame;//生成的html文件保存路径。D:\\TestFile\\checkfile\\ --/usr/dnfile/checkfile/ logger.debug("====================文件名1====================" + fileame); byte[] bytes = fileame.getBytes("UTF-8"); String f = new String(bytes,"UTF-8"); logger.debug("====================文件名2====================" + f);// logger.debug("====================templateContent2====================" + templateContent2); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f),"UTF-8")); writer.write(templateContent); writer.flush(); writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
向Linux写入html文件就完成了。
有个问题一直没解决就是生成的html文件,文件名带有中文时会乱码,改了服务器编码也没有解决。生成文件名时做了格式限制也没解决。0-0
阅读全文
0 0
- JAVA读取模板,向Linux服务器写入html文件
- file 读取写入文件 HTML
- java文件读取、写入
- Hadoop向HDFS写入、删除、读取文件
- java向文件写入数据
- java向FTP写入文件
- java创建文件,并向文件中写入字符串,读取字符串到屏幕
- JAVA中创建ZIP文件,并向ZIP文件中写入并读取条目和数据
- excel 读取模板,病写入新文件
- java文件写入与读取
- java 读取 写入 csv 文件
- java读取/写入属性文件
- Java读取和写入文件
- java读取写入.cvs文件
- java写入和读取文件
- Java文件读取,写入数据库
- C#读取HTML文件内容写入记事本
- java访问Linux服务器读取文件
- [vim] 删除
- 类名.class, class.forName(), getClass()区别
- 修改mysql默认字符集为utf
- Hadoop HA 环境搭建
- android 6.0(api 23) SDK,不再提供org.apache.http.*(只保留几个类)
- JAVA读取模板,向Linux服务器写入html文件
- Mybatis级联删除、修改、查询问题
- 源码:我的关于NLP的博客(持续更新中...)
- 互联网广告综述之点击率特征工程
- 观看书籍推荐
- nginx简易教程
- 天生爱表演的ESFP职业规划和天赋解析
- soap的命名空间
- python+Selenium2+chrome构建浏览器模拟环境