您现在的位置: 天极网 > 开发频道 > web开发 > 在JSP中操作文件
全文

在JSP中操作文件

2002-04-23 17:37作者:包路跃出处:Yesky责任编辑:

  一、前言

  该例子涉及到文件的读写和jpg图片的自动生成。利用jsp+servlet的技术,jsp调用servlet生成图片。

  二、首文件index.jsp如下

<%@ page contentType="text/html;charSet=gb2312"%>
<%
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires",0);
%>
<%!
//getQuestion(String)函数用来获得文本文件中的问题
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 = 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



<%!
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 :



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的值。



question.txt:
Yes,I think so!
No,I dont think so!
Sorry,I dont know the answer!

count.txt:
12
9
5
9
共6页。 9 1 2 3 4 5 6 :

软件资讯·软件下载尽在天极软件

相关搜索:
相关文章及软件
关注此文读者还看过
热门关注
特别推荐
网友关注
软件下载
娱乐下载
驱动下载
文章排行
本周
本月
最近更新
关于我们|About us|网站律师|天极服务|电子杂志|RSS订阅|加入我们|网站地图
TMG
Copyright (C) 1999-2009 Chinabyte.com, All Rights Reserved 版权所有 天极网络
商务联系、网站内容、合作建议:010-82657868
版权声明 在线提交意见反馈 渝ICP证B2-20030003号
经营性网站备案信息 网警备案 中国网站排名
天极传媒:天极网|比特网|IT专家网|IT商网|52PK游戏网|IT分众