精品中文字幕在线,久久精品成人,伊人久久综合一区二区http://www.aygfsteel.com/yundixiaoduo/zh-cnWed, 18 Jun 2025 09:37:45 GMTWed, 18 Jun 2025 09:37:45 GMT60用 img 標簽 實現 網頁計數器http://www.aygfsteel.com/yundixiaoduo/archive/2010/12/02/339612.html云帝xiao墮云帝xiao墮Thu, 02 Dec 2010 07:21:00 GMThttp://www.aygfsteel.com/yundixiaoduo/archive/2010/12/02/339612.htmlhttp://www.aygfsteel.com/yundixiaoduo/comments/339612.htmlhttp://www.aygfsteel.com/yundixiaoduo/archive/2010/12/02/339612.html#Feedback0http://www.aygfsteel.com/yundixiaoduo/comments/commentRss/339612.htmlhttp://www.aygfsteel.com/yundixiaoduo/services/trackbacks/339612.html以下是CountServlet.java:
 1 import java.awt.Color;
 2 import java.awt.Font;
 3 import java.awt.Graphics;
 4 import java.awt.image.BufferedImage;
 5 import java.io.ByteArrayOutputStream;
 6 import java.io.FileInputStream;
 7 import java.io.FileOutputStream;
 8 import java.io.IOException;
 9 import java.util.Properties;
10 
11 import javax.imageio.ImageIO;
12 import javax.servlet.ServletException;
13 import javax.servlet.ServletOutputStream;
14 import javax.servlet.http.HttpServlet;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17 
18 public class CountServlet extends HttpServlet {
19 
20     private static final long serialVersionUID = 1L;
21 
22     public void doGet(HttpServletRequest request, HttpServletResponse response)
23             throws ServletException, IOException {
24         response.setContentType("image/jpeg");
25         // 設置瀏覽器不要緩存此圖片
26         response.setHeader("Pragma""No-cache");
27         response.setHeader("Cache-Control""no-cache");
28         response.setDateHeader("Expires"0);
29 
30         ServletOutputStream sos = response.getOutputStream();
31 
32         BufferedImage image = new BufferedImage(8020,
33                 BufferedImage.TYPE_INT_RGB);
34         Graphics g = image.getGraphics();
35 
36         g.setColor(Color.WHITE);
37         g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));
38         String value = getAccessCount(request);
39         int len = value.length();
40         // 最大訪問次數不超過9999999
41         if (len > 7) {
42             value = "9999999";
43         } else {
44             String tmp = "";
45             for (int i = 0; i < 7 - len; i++) {
46                 tmp = tmp + "0";
47             }
48             value = tmp + value;
49         }
50         g.drawString(value, 018);
51         // 結束圖像的繪制過程,完成圖像
52         g.dispose();
53         // 避免因圖像大小超過Servlet引擎輸出緩沖區的大小而自動使用chunked編碼
54         ByteArrayOutputStream bos = new ByteArrayOutputStream();
55         ImageIO.write(image, "JPEG", bos);
56         byte[] buf = bos.toByteArray();
57         response.setContentLength(buf.length);
58         bos.close();
59         sos.write(buf);
60         sos.close();
61     }
62 
63     private String getAccessCount(HttpServletRequest request) {
64         // 得到引用頁面的URL地址,并以此作為訪問次數的檢索關鍵字
65         String pageKey = request.getHeader("referer");
66         if (pageKey == null) {
67             return "0";
68         }
69 
70         Properties settings = new Properties();
71         String countFilePath = getServletContext().getRealPath("/count.txt");
72         try {
73             FileInputStream fi = new FileInputStream(countFilePath);
74             settings.load(fi);
75             fi.close();
76         } catch (Exception e) {
77             e.printStackTrace();
78         }
79         String count = "0";
80         try {
81             count = settings.getProperty(pageKey);
82             if (count == null) {
83                 count = "0";
84             }
85             int c = Integer.parseInt(count) + 1;
86             count = new Integer(c).toString();
87             settings.put(pageKey, count);
88             FileOutputStream fi = new FileOutputStream(countFilePath);
89             settings.store(fi, "the page is accessed:");
90             fi.close();
91         } catch (Exception e) {
92             e.printStackTrace();
93         }
94         return count;
95     }
96 }
以下是Count.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    本網頁已被訪問了
<img src="/servlet/CountServlet">
</body>
</html>



云帝xiao墮 2010-12-02 15:21 發表評論
]]>
主站蜘蛛池模板: 安国市| 合川市| 承德县| 明溪县| 睢宁县| 托里县| 文登市| 七台河市| 惠州市| 搜索| 土默特左旗| 敦化市| 宁远县| 永川市| 桂阳县| 韶山市| 永新县| 敦化市| 嘉义县| 星座| 阆中市| 安义县| 通化市| 奎屯市| 淮阳县| 乐东| 青州市| 安泽县| 中江县| 阿拉善右旗| 太保市| 莱西市| 六枝特区| 彭泽县| 中江县| 玛沁县| 鄂托克前旗| 涡阳县| 咸丰县| 胶南市| 巨野县|