生成图片流
package
com.bo;

import
java.awt.Color;
import
java.awt.Font;
import
java.awt.Graphics;
import
java.awt.image.BufferedImage;
import
java.io.IOException;
import
java.io.OutputStream;
import
java.util.Random;

import
javax.imageio.ImageIO;


public
class
ImageOP
...
{
private Random random;
private BufferedImage image;
private Font font;
private int distance;

public ImageOP(int width,int heigh,String fontStr,int sytle,int size,int distance)...{
random = new Random();
image = new BufferedImage(width,heigh,BufferedImage.TYPE_3BYTE_BGR);
font = new Font(fontStr,sytle,size);
this.distance = distance;
}
//默认格式

public ImageOP()...{
this(75,25,"宋体",Font.BOLD,25,20);
}

public String getCheckCodeImage(String str,int show,OutputStream output) throws IOException...{
Graphics d = image.getGraphics();
d.setColor(Color.WHITE);
d.fillRect(0,0,image.getWidth(),image.getHeight());
d.setColor(new Color(random.nextInt(100)+100,random.nextInt(100)+100,random.nextInt(100)+100));

for (int i = 0; i < 10; i++) ...{
d.drawLine(random.nextInt(image.getWidth()),random.nextInt(image.getHeight()),random.nextInt(image.getWidth()),random.nextInt(image.getHeight()));
}
d.setColor(Color.BLACK);
d.setFont(font);
String checkCode = "";
char tmp = 0;
int x = -distance;

for (int i = 0; i < show; i++) ...{
tmp = str.charAt(random.nextInt(str.length()-1));
checkCode = checkCode + tmp;
x = x + distance;
d.setColor(new Color(random.nextInt(100)+50,random.nextInt(100)+50,random.nextInt(100)+50));
d.drawString(tmp+"",x,random.nextInt(image.getHeight()-(font.getSize()/2))+(font.getSize()/2));
}
d.dispose();
ImageIO.write(image,"jpeg",output);
return checkCode;
}
}
public
ActionForward getCheckCode(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,

HttpServletResponse response)
...
{

try ...{
ImageOP op = new ImageOP();
HttpSession session = request.getSession();
session.setAttribute("checkCode",op.getCheckCodeImage("abcdefghigklmnopqrst0123456789",4,response.getOutputStream()));

} catch (IOException e) ...{
e.printStackTrace();
}
return null;
}
<
action
path
="/checkImage"
type
="com.action.LogonAction"<!-- action调用上面的方法 -->
name
=""
scope
="request"
input
=""
parameter
="opName"
validate
="false"
>
</
action
>
<
html
>

<
script
language
="JavaScript"
>
...

function open_window() ...{
window.location.reload();
}
</
script
>
<
head
>
<
base
href
="<%=basePath%>"
>
<
title
>
My JSP 'son.jsp' starting page
</
title
>
<
meta
http-equiv
="pragma"
content
="no-cache"
>
<
meta
http-equiv
="cache-control"
content
="no-cache"
>
<
meta
http-equiv
="expires"
content
="0"
>
<
meta
http-equiv
="keywords"
content
="keyword1,keyword2,keyword3"
>
<
meta
http-equiv
="description"
content
="This is my page"
>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</
head
>
<
body
>
<
a
href
="#"
onclick
="open_window();"
class
="global"
><
img
src
="checkImage.do?opName=getCheckCode"
border
="0"
></
a
><!-- 点击图片能够刷新的图片 -->
</
body
>
</
html
>
欢迎交流































































action调用的方法
















struts-config.xml配置








页面加载




























