JSP生成jpeg图片用于投票
程序员文章站
2024-03-31 11:51:04
一、前言 本文原作者为tony wang ,该文章涉及到文件的读写...
一、前言
本文原作者为tony wang ,该文章涉及到文件的读写和jpg图片的自动生成。利用jsp+servlet的技术,jsp调用servlet生成图片。
二、首文件index.jsp如下:
<%--
author: tony wang
e-mail: lucky_tony@163.net
date: 2001-01-01
如果对程序有什么疑问,可以和我联系, 另外程序如果有什么bug,麻烦指出!!
--%>
<%@ 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>冰帆调查</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="我 投 一 票"></td>
</tr>
<tr>
<td align=center><img src="/vote/servlet/voteimage" width=150 height=100></td>
</tr>
</form>
</table>
</body>
</html>
三、写文件write.jsp
<%--
author: tony wang
e-mail: lucky_tony@163.net
date: 2001-01-01
如果对程序有什么疑问,可以和我联系,
另外程序如果有什么bug,麻烦指出!!
--%>
<%!
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");
%>
四、servlet原代码:voteimage.java :
/*
author: tony wang
e-mail: lucky_tony@163.net
date: 2001-01-01
如果对程序有什么疑问,可以和我联系,
另外程序如果有什么bug,麻烦指出!!
*/
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(at);
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(10,24,80,50,fromang,ang[i],arc2d.pie);
g.fill(arc);
fromang+=ang[i];
if (fromang>360)
{
fromang-=360;
}
}
at = affinetransform.getrotateinstance(0,arc.getcenterx(),arc.
getcentery());
g.settransform(at);
for (int i=0;i<4;i++){
g.setcolor(color[i]);
g.fillrect(100,15*i+20,10,10);
g.drawstring((char)("a"+i)+"",120,15*i+20+8);
}
jpegimageencoder encoder=jpegcodec.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;
}
}
五、在index.jsp目录下建立question.txt和count.txt文件分别用来保存投
票的问题和投票的数量,用户投票后,就修改count.txt的值。
为了对原作者表示感谢,这2个文件内容不变化,如下:
question.txt:
yes,i think so!
no,i dont think so!
sorry,i dont know the answer!
count.txt:
12
9
5
9
六、目录结构:
(1)jsp文件和txt文件同一个目录
(2).java文件是servlet目录下
七、测试:
http://[server:port]/dir/index.jsp