【 tulaoshi.com - Java 】
一、前言
本文原作者为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;
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;
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="http://img.jcwcn.com/attachment/portal" 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;
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;
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