JAVA读取模板,向Linux服务器写入html文件

需求:对不同岗位的员工进行线上考核,然后生成考核文件(以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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值