webservie 客户端读取服务器端日志例子(以网页展现)

package bpmlog;

import java.io.BufferedInputStream;
下面是一个完整的 servlet,直接复制它既可以使用,只需要修改红色部分路径即可,

本例使用方法:在浏览器直接键入URL:即可展现日志,如下:http://localhost:8888/BPMDemo/BPMClientLogService?point=p1


代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BPMClientLogService extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 1L;

/**
* Constructor of the object.
*/
public BPMClientLogService() {
super();
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String point = request.getParameter("point");
String fileName = "\\bpm.log";
String filePath = "C:\\BPM_Logs\\"+point+fileName;
if(point.equals("")){
errorHttpContent(response);
}else{
showLogContent(response,filePath);
}
}



private void errorHttpContent(HttpServletResponse response) throws IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>BPMClientLogService</TITLE></HEAD>");
out.println("  <BODY>");
out.println(createTitle());
out.println(createBar());
out.println("Reason: did not find the relevant connection point!!!");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}




private void showLogContent(HttpServletResponse response,String filePath) throws IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>BPMClientLogService</TITLE></HEAD>");
out.println("  <BODY>");
//----------------------------------------------------------------------------------

out.println(createTitle());
out.println(createBar());
String httpContent = createHttpContent(filePath);
if(!httpContent.equals("")){
out.println(createHttpContent(filePath));
}else{
out.println("Reason: did not find the relevant connection point!!!");
}


//----------------------------------------------------------------------------------
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}



private String createHttpContent(String filePathAndName) {
String httpContent = "";
InputStreamReader inr = null;
BufferedReader reader = null;
try {
File f = new File(filePathAndName);
if (f.isFile() && f.exists()) {
inr = new InputStreamReader(new FileInputStream(f), get_charset(f));
reader = new BufferedReader(inr);
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("20")) {
httpContent += createLine();
httpContent += createLine(line);
} else {
httpContent += createLine(line);
}
}
}
} catch (Exception e) {
System.out.println("读取文件内容操作出错");
e.printStackTrace();
} finally {
close(reader, inr);
}
return httpContent;
}



private void close(BufferedReader reader, InputStreamReader inr) {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (inr != null)
inr.close();
} catch (IOException e) {
e.printStackTrace();
}

}



private String createLine(String content){
System.out.println("<h4>"+content+"</h4>");
return "<h4>"+content+"</h4>";
}



private String createLine(){
return "<br/><br/>";
}



private String createBar(){
return "<hr width='60%' size='10' align='center' color='#9900ff'/>";
}


private String createTitle() {
         return "<h2  style='color: #9900ff;font-weight: 400;' align='center'>BPMPlatform  client-side  logging service ! ! !</h2>";
}


private String get_charset(File file) {
String charset = "GBK";
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1)
return charset;
if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
charset = "UTF-16LE";
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE
&& first3Bytes[1] == (byte) 0xFF) {
charset = "UTF-16BE";
checked = true;
} else if (first3Bytes[0] == (byte) 0xEF
&& first3Bytes[1] == (byte) 0xBB
&& first3Bytes[2] == (byte) 0xBF) {
charset = "UTF-8";
checked = true;
}
bis.reset();
if (!checked) {
int len = 0;
int loc = 0;

while ((read = bis.read()) != -1) {
loc++;
if (read >= 0xF0)
break;
if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK
break;
if (0xC0 <= read && read <= 0xDF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF)// 双字节 (0xC0 - 0xDF)
// (0x80 -
// 0xBF),也可能在GB编码内
continue;
else
break;
} else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
charset = "UTF-8";
break;
} else
break;
} else
break;
}
}
System.out.println(loc + " " + Integer.toHexString(read));
}

bis.close();
} catch (Exception e) {
e.printStackTrace();
}

return charset;
}


/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}




}

你可能感兴趣的:(html,servlet,浏览器,F#)