源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
查看: 390|回复: 14

[JSP编程] JSP生成jpeg图片用于投票

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2006-10-13 00:00: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







六、目录结构:



(1)jsp文件和txt文件同一个目录



(2).java文件是servlet目录下



七、测试:



http://[server:port]/dir/index.jsp
回复

使用道具 举报

0

主题

1万

回帖

68

积分

注册会员

Rank: 2

积分
68
发表于 2022-8-8 18:09:08 | 显示全部楼层
的沙发水电费水电费
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-6-23 23:44:10 | 显示全部楼层
好东西可以可以可以可以
回复 支持 反对

使用道具 举报

8

主题

1万

回帖

52

积分

注册会员

Rank: 2

积分
52
发表于 2023-6-24 03:30:44 | 显示全部楼层
先把创新班才能下班才能下班
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-8-28 15:44:37 | 显示全部楼层
8888888888888888
回复 支持 反对

使用道具 举报

4

主题

1万

回帖

303

积分

中级会员

Rank: 3Rank: 3

积分
303
发表于 2023-10-8 23:21:33 | 显示全部楼层
8888888888888888
回复 支持 反对

使用道具 举报

6

主题

1万

回帖

247

积分

中级会员

Rank: 3Rank: 3

积分
247
发表于 2023-11-26 09:10:10 | 显示全部楼层
来看看!!!
回复 支持 反对

使用道具 举报

14

主题

1万

回帖

75

积分

注册会员

Rank: 2

积分
75
发表于 2024-3-2 06:29:59 | 显示全部楼层
儿童服务绯闻绯闻绯闻
回复 支持 反对

使用道具 举报

2

主题

1万

回帖

473

积分

中级会员

Rank: 3Rank: 3

积分
473
发表于 2024-3-5 09:50:29 | 显示全部楼层
为全额万千瓦
回复 支持 反对

使用道具 举报

15

主题

1万

回帖

122

积分

注册会员

Rank: 2

积分
122
发表于 2024-6-6 12:01:38 | 显示全部楼层
sdsadsadsadf
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-9-21 08:28 , Processed in 0.078400 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表