response生成图片验证码
- - ITeye博客服务器对客户端浏览器做出的响应被封装成一个HttpResponse对象. 要对浏览器进行操作,只需要操作HttpRespones对象,通过HttpServletResponse.getWriter()获得PrintWriter,该对象为outputStream的子类. 本例将使用Servlet输出图片验证码.
import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class IdentityServlet extends HttpServlet{ public static final char[] CHARS={'2','3','4','5','6','7','8','9','A','B','C', 'D','E','F','G','H','K','M','L','N','L','X','Y','Z','Q'}; public static Random random=new Random(); public static String getRandomString(){ StringBuffer buffer=new StringBuffer(); //随即产生6位数 for(int i=0;i<6;i++){ buffer.append(CHARS[random.nextInt(CHARS.length)]); } System.out.println(buffer.toString()); return buffer.toString(); } //获取随即的颜色 public static Color getRandomColor(){ return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)); } //返回某颜色的反色 public static Color getReverseColor(Color c){ return new Color(255-c.getRed(), 255-c.getGreen(),255-c.getBlue()); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg"); String randomString=getRandomString(); request.getSession(true).setAttribute("randomString", randomString); int width=100;//图片宽度 int height=30;//图片高度 Color color=getRandomColor(); //随即颜色。用于背景颜色 Color reverse=getReverseColor(color);//反色,用于前背景 //创建一个彩色图片 BufferedImage bi=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //获取绘图对象 Graphics2D g=bi.createGraphics(); g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));//设置字体 g.setColor(color); //设置颜色 g.fillRect(0, 0, width, height);//绘制背景 g.setColor(reverse);//设置颜色 g.drawString(randomString, 18, 20); for(int i=0;i<100;i++){ g.drawRect(random.nextInt(width), random.nextInt(height),1, 1); //随即噪音点 } ServletOutputStream out=response.getOutputStream(); //转换JPEG格式 JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out); //编码器 encoder.encode(bi); out.flush(); //输出到客户端 } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doPost(req, resp); } }
<servlet> <servlet-name>IdentityServlet</servlet-name> <servlet-class>com.fcl.IdentityServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>IdentityServlet</servlet-name> <url-pattern>/servlet/IdentityServlet</url-pattern> </servlet-mapping>
<script type="text/javascript"> function reloadImage(){ document.getElementById('btn').disabled=true; document.getElementById('identity').src='servlet/IdentityServlet?ts='+new Date().getTime(); } </script>
<body> <img src="servlet/IdentityServlet" id="identity" onload="btn.disabled=false;"> <input type="button" value="更换图片" onclick="reloadImage()" id="btn"> </body>