An example of a graphic counter

Source: Internet
Author: User

Import java. io .*;
Import java. util .*;
Import com.sun.image.codec.jpeg .*;
Import javax. servlet .*;
Import javax. servlet. http .*;

Import java. awt .*;
Import java. awt. image .*;
Public class extends servlet extends HttpServlet {

Private String strFile = "c: count.txt ";
Private Font mFont = new Font ("Courier", Font. BOLD, 12 );;

Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {

String s;
Int intCount;
S = getCount ();
IntCount = new Integer (s). intValue ();
IntCount = intCount + 1;
WriteCount (intCount + "");

Response. setContentType ("image/jpeg ");
ServletOutputStream out = response. getOutputStream ();
BufferedImage image = new BufferedImage (110,16, BufferedImage. TYPE_INT_RGB );
Graphics g = image. getGraphics ();
G. setColor (Color. black );
G. fillRect );
G. setColor (Color. white );
G. setFont (mFont );

Char c;
For (int I = 0; I <10; I ++ ){
C = s. charAt (I );
G. drawString (c + "", I * 11 + 1, 12 );
G. drawLine (I + 1) * 11-, (I + 1) * 11 );
}

Required imageencoder encoder = required codec. createJPEGEncoder (out );
Encoder. encode (image );
Out. close ();
}

Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {

DoGet (request, response );
}

Public synchronized void writeCount (String content ){
Try {
PrintWriter pw = new PrintWriter (new FileOutputStream (strFile ));
Pw. println (content );
Pw. close ();
} Catch (IOException e ){
System. out. println ("Write file error:" + e. getMessage ());
}
}

Public synchronized String getCount (){
String strCount;
Int len;
Int leftLen;

Try {
BufferedReader inf = new BufferedReader (new FileReader (strFile ));
StrCount = inf. readLine ();
Inf. close ();
} Catch (Exception e ){
System. out. println ("Get counter error:" + e. getMessage ());
Return reChar ('0', 10 );
}
Len = strCount. length ();
If (len <= 0 ){
Return reChar ('0', 10 );
}

If (len> 10 ){
Return reChar ('9', 10 );
}

LeftLen = 10-len;
Return reChar ('0', leftLen) + strCount;
}

Private String reChar (char c, int num ){
Int I;
If (num <= 0) return "";
String s = "";
For (I = 0; I <num; I ++ ){
S = s + c;
}
Return s;
}
}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.