I. Preface
The original author of this article is Tony Wang. This article involves file read/write and automatic jpg image generation. Using jsp + servlet technology, jsp calls servlet to generate images.
Ii. index. jsp of the first file is as follows:
<% --
Author: Tony Wang
E-mail: lucky_tony@163.net
Date: 2001-001
If you have any questions about the program, contact me. If the program has any bugs, please point out !!
-- %>
<% @ Page contentType = "text/html; charSet = gb2312" %>
<%
Response. setHeader ("Cache-Control", "no-store ");
Response. setDateHeader ("Expires", 0 );
%>
<%!
Public String [] getQuestion (String s)
{
String [] strQ = new String [4];
String strTemp = null;
Int I;
Java. io. RandomAccessFile rf = null;
Try {
Rf = new java. io. RandomAccessFile (s, "r ");
} Catch (Exception e)
{
System. out. println (e );
System. exit (0 );
}
For (I = 0; I <4; I ++)
{
Try {
StrTemp = rf. readLine ();
} Catch (Exception e ){
StrTemp = "None Question ";
}
If (strTemp = null) strTemp = "None Question ";
StrQ [I] = strTemp;
}
Return strQ;
}
%>
<%
String s = null;
String [] question = new String [4];
S = request. getRealPath ("question.txt ");
Question = getQuestion (s );
%>
<Html>
<Head>
<Title> </title>
<Link href = "css.css" rel = "StyleSheet" type = "text/css"> </link>
</Head>
<Body>
<Table width = "180" border = "1" bordercolor = "#999999">
<Tr>
<Td align = center> ice sail survey </td>
</Tr>
<Form name = frm method = post action = write. jsp>
<Tr>
<Td>
<%
String ss = null;
For (int I = 0; I <4; I ++)
{
Ss = "<input type = \" radio \ "name = \" choice \ "value =" + I + ">" +
(Char) ("A" + I) + "," + question [I] + "<br> ";
Out. println (ss );
}
%>
</Td>
</Tr>
<Tr>
<Td align = center> <input type = submit value = "I vote"> </td>
</Tr>
<Tr>
<Td align = center> </td>
</Tr>
</Form>
</Table>
</Body>
</Html>
3. write File write. jsp
<% --
Author: Tony Wang
E-mail: lucky_tony@163.net
Date: 2001-001
If you have any questions about the program, contact me,
In addition, if the program has any bugs, please point it out !!
-- %>
<%!
Public int [] getNumber (String s)
{
Int [] mCount = new int [4];
String strTemp = null;
Int I;
Java. io. RandomAccessFile rf = null;
Try {
Rf = new java. io. RandomAccessFile (s, "r ");
} Catch (Exception e)
{
System. out. println (e );
System. exit (0 );
}
For (I = 0; I <4; I ++)
{
Try {
StrTemp = rf. readLine ();
} Catch (Exception e ){
StrTemp = "0 ";
}
If (strTemp = null) strTemp = "0 ";
MCount [I] = new Integer (strTemp). intValue ();
}
Return mCount;
}
Public void setNumber (String s, int [] x)
{
Try {
Java. io. PrintWriter pw = new java. io. PrintWriter (new java. io.
FileOutputStream (s ));
For (int I = 0; I <4; I ++ ){
Pw. println (x [I] + "");
}
Pw. close ();
} Catch (Exception e ){
System. out. println ("Write file error:" + e. getMessage ());
}
}
%>
<%
String tmp = null;
Int choice =-1;
Int [] count = new int [4];
Tmp = request. getParameter ("choice ");
If (tmp = null ){
} Else {
Choice = new Integer (tmp). intValue ();
}
/////////////
String s = request. getRealPath ("count.txt ");
If (choice> = 0 ){
Count = getNumber (s );
Count [choice] ++;
SetNumber (s, count );
}
Response. sendRedirect ("index. jsp ");
%>
4. Source servlet code: VoteImage. java:
/*
Author: Tony Wang
E-mail: lucky_tony@163.net
Date: 2001-001
If you have any questions about the program, contact me,
In addition, if the program has any bugs, please point it out !!
*/
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. geom .*;
Import java. awt. image .*;
Public class VoteImage extends HttpServlet
{
Private String strFile = null;
Private Color color [] = {Color. red, Color. black, Color. orange,
Color. green };
Private int baseAng = 30;
Public void doGet (HttpServletRequest request, HttpServletResponse
Response)
Throws ServletException, IOException
{
StrFile = request. getRealPath ("count.txt ");
Float [] [] xy = new float [4] [2];
Xy = getNumAndPercent (strFile );
Int [] ang = new int [4];
Ang [0] = (int) (xy [0] [1] * 360 );
Ang [1] = (int) (xy [1] [1] * 360 );
Ang [2] = (int) (xy [2] [1] * 360 );
Ang [3] = 360-ang [0]-ang [1]-ang [2];
Response. setHeader ("Cache-Control", "no-store ");
Response. setDateHeader ("Expires", 0 );
Response. setContentType ("image/jpeg ");
ServletOutputStream out = response. getOutputStream ();
BufferedImage image = new BufferedImage (150,100, BufferedImage.
TYPE_INT_RGB );
Graphics2D g = (Graphics2D) image. getGraphics ();
G. setRenderingHint (RenderingHints. KEY_ANTIALIASING,
RenderingHints. VALUE_ANTIALIAS_ON );
G. setColor (Color. white );
G. fillRect (0, 0, 150,100 );
AffineTransform at = null;
Arc2D arc = null;
Int fromAng = baseAng;
At = AffineTransform. getRotateInstance (-20 * java. lang. Math. PI)
/180,45, 37 );
G. setTransform ();
Int r = 6;
Int dx = (int) (r * java. lang. Math. cos (baseAng + ang [0])/2.0 * java.
Lang. Math. PI/180 ));
Int dy = (int) (r * java. lang. Math. sin (baseAng + ang [0])/2.0 * java.
Lang. Math. PI/180 ));
Arc = new Arc2D. Double (10 + dx, 24-dy, 80, 50, fromAng, ang [0], Arc2D. PIE );
G. setColor (color [0]);
G. fill (arc );
FromAng + = ang [0];
For (int I = 1; I <4; I ++)
{
G. setColor (color [I]);
Arc = new Arc2D. Double (, 80, 50, fromAng, ang [I], Arc2D. PIE );
G. fill (arc );
FromAng + = ang [I];
If (fromAng> 360)
{
From g-= 360;
}
}
At = AffineTransform. getRotateInstance (0, arc. getCenterX (), arc.
GetCenterY ());
G. setTransform ();
For (int I = 0; I <4; I ++ ){
G. setColor (color [I]);
G. fillRect (* I + 20, 10, 10 );
G. drawString (char) ("A" + I) + "", 120,15 x I + 20 + 8 );
}
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 float [] [] getNumAndPercent (String sFileName)
{
Float xx [] [] = new float [4] [2];
Int totalNum = 0;
String strTemp = null;
Int I = 0;
Java. io. RandomAccessFile rf = null;
Try
{
Rf = new java. io. RandomAccessFile (sFileName, "r ");
} Catch (Exception e)
{
System. out. println (e );
System. exit (0 );
}
For (I = 0; I <4; I ++)
{
Int m = 0;
Try {
StrTemp = rf. readLine ();
} Catch (Exception e ){
StrTemp = "0 ";
}
If (strTemp = null) strTemp = "0 ";
M = new Integer (strTemp). intValue ();
Xx [I] [0] = m;
TotalNum + = m;
}
If (totalNum = 0) totalNum = 1;
For (I = 0; I <4; I ++ ){
Xx [I] [1] = xx [I] [0]/totalNum;
}
Return xx;
}
}
The question.txtand count.txt files are created in index.jsp respectively to save
After the user votes, the count.txt value is changed.
To thank the original author, the contents of these two files remain unchanged, as shown below:
Question.txt:
Yes, I think so!
No, I dont think so!
Sorry, I dont know the answer!
Count.txt:
12
9
5
9
Vi. directory structure:
(1) The jsp file and txt file are in the same directory.
(2) the java file is under the servlet directory.
VII. test:
Http: // [server: port]/dir/index. jsp