动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   Jsp → 阅读文章

 JSP生成jpeg图片用于投票

作者来源: 
阅读 数 244 人次 , 2006-3-29 4:23:00 


一、前言

   本文原作者为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

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:jsp中实现分页
· 下一篇:Struts的动态表单的应用
· 在Linux环境下安装JSP(二)
· JSP入门教程(3)
· 在jsp中作HTTP认证的方法
· 在Windows NT 4.0下安装Apache+Servlet+JSP 
· 让JSP页面过期, 每次JSP页面都是最新


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077