对于以上的代码样式,想必大家非常熟悉,下面给出一个参考示例:
<%@page import="java.io.InputStream"%>
<%@page import="com.baiyang.lc.util.FileUtil"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String docRoot = System.getProperty("docRoot");
String file = request.getParameter("path");
String type = file.substring(file.lastIndexOf(".") + 1)
.toLowerCase();
if (type == "cs") {
type = "CSharp";
} else if ("txt".equals(type)) {
type = "plain";
}
String brush = type;
type = type.substring(0, 1).toUpperCase() + type.substring(1);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/sh/shCore.js"></script>
<script type="text/javascript" src="js/sh/shBrush<%=type%>.js"></script>
<link type="text/css" rel="stylesheet" href="css/sh/shCoreDefault.css" />
<script type="text/javascript">
SyntaxHighlighter.all();
</script>
</head>
<body>
<pre class="brush: <%=brush%>;">
<%
String encoding=FileUtil.getEncoding(docRoot+file);
System.out.println(encoding);
InputStream is = new FileInputStream(docRoot+file);
byte[] bs = new byte[1024];
int len = 0;
String s=null;
while ((len = is.read(bs)) != -1) {
if(encoding!=null&&encoding.trim().length()!=0){
s=new String(bs, 0, len, encoding);
}else{
s=new String(bs,0,len);
}
%>
<%=s%>
<%
}
is.close();
%>
</pre>
</body>
</html>
请注意代码中的文件路径,所需的控件为
SyntaxHighlighter