<一>、Java生成验证码图片 1.Servlet生成验证码图片
- package com.logcd.servlet;
-
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import java.util.Random;
- import javax.imageio.ImageIO;
- import javax.servlet.*;
- import java.io.*;
- import javax.servlet.http.*;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- @SuppressWarnings("serial")
- public class RandomCode extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- this.doPost(request, response);
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
-
- int width = 70;
-
- int height = 30;
- BufferedImage buffImg = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- Graphics2D g = buffImg.createGraphics();
-
-
- Random random = new Random();
-
-
- g.setColor(getRandColor(200, 250));
- g.fillRect(0, 0, width, height);
-
- Font font = new Font("Times New Roman", Font.HANGING_BASELINE, 28);
-
- g.setFont(font);
-
-
- g.setColor(Color.BLACK);
- g.drawRect(0, 0, width - 1, height - 1);
-
-
- g.setColor(getRandColor(160,200));
- for (int i = 0; i < 155; i++) {
- int x = random.nextInt(width);
- int y = random.nextInt(height);
- int xl = random.nextInt(12);
- int yl = random.nextInt(12);
- g.drawLine(x, y, x + xl, y + yl);
- }
-
-
- StringBuffer randomCode = new StringBuffer();
-
-
- int length = 4;
-
- String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
-
- int size = base.length();
-
-
- for (int i = 0; i < length; i++) {
-
- int start = random.nextInt(size);
- String strRand = base.substring(start, start + 1);
-
-
-
-
-
-
- g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
-
- g.drawString(strRand, 15 * i + 6, 24);
-
-
- randomCode.append(strRand);
- }
-
- HttpSession session = request.getSession();
- session.setAttribute("rand", randomCode.toString());
-
-
- g.dispose();
-
-
- response.setHeader("Pragma", "no-cache");
- response.setHeader("Cache-Control", "no-cache");
- response.setDateHeader("Expires", 0);
-
- response.setContentType("image/jpeg");
-
-
- ServletOutputStream sos = response.getOutputStream();
- ImageIO.write(buffImg, "jpeg", sos);
- sos.flush();
- sos.close();
-
- }
-
- Color getRandColor(int fc, int bc) {
- Random random = new Random();
- if (fc > 255)
- fc = 255;
- if (bc > 255)
- bc = 255;
- int r = fc + random.nextInt(bc - fc);
- int g = fc + random.nextInt(bc - fc);
- int b = fc + random.nextInt(bc - fc);
- return new Color(r, g, b);
- }
-
- }
package com.logcd.servlet;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.*;import java.io.*;import javax.servlet.http.*;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@SuppressWarnings("serial")public class RandomCode extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 验证码图片的宽度。int width = 70;// 验证码图片的高度。int height = 30;BufferedImage buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics2D g = buffImg.createGraphics();// 创建一个随机数生成器类。Random random = new Random();// 设定图像背景色(因为是做背景,所以偏淡)g.setColor(getRandColor(200, 250));g.fillRect(0, 0, width, height);// 创建字体,字体的大小应该根据图片的高度来定。Font font = new Font("Times New Roman", Font.HANGING_BASELINE, 28);// 设置字体。g.setFont(font);// 画边框。g.setColor(Color.BLACK);g.drawRect(0, 0, width - 1, height - 1);// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到。//g.setColor(Color.GRAY);g.setColor(getRandColor(160,200));for (int i = 0; i < 155; i++) {int x = random.nextInt(width);int y = random.nextInt(height);int xl = random.nextInt(12);int yl = random.nextInt(12);g.drawLine(x, y, x + xl, y + yl);}// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。StringBuffer randomCode = new StringBuffer();// 设置默认生成4个验证码int length = 4;// 设置备选验证码:包括"a-z"和数字"0-9"String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";int size = base.length();// 随机产生4位数字的验证码。for (int i = 0; i < length; i++) {// 得到随机产生的验证码数字。int start = random.nextInt(size);String strRand = base.substring(start, start + 1);// 用随机产生的颜色将验证码绘制到图像中。// 生成随机颜色(因为是做前景,所以偏深)//g.setColor(getRandColor(1, 100));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));g.drawString(strRand, 15 * i + 6, 24);// 将产生的四个随机数组合在一起。randomCode.append(strRand);}// 将四位数字的验证码保存到Session中。HttpSession session = request.getSession();session.setAttribute("rand", randomCode.toString());//图象生效 g.dispose();// 禁止图像缓存。response.setHeader("Pragma", "no-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0);response.setContentType("image/jpeg");// 将图像输出到Servlet输出流中。ServletOutputStream sos = response.getOutputStream();ImageIO.write(buffImg, "jpeg", sos);sos.flush();sos.close();}Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色Random random = new Random();if (fc > 255)fc = 255;if (bc > 255)bc = 255;int r = fc + random.nextInt(bc - fc);int g = fc + random.nextInt(bc - fc);int b = fc + random.nextInt(bc - fc);return new Color(r, g, b);}}
2.配置
- <servlet>
- <servlet-name>RandomCode</servlet-name>
- <servlet-class>com.logcd.servlet.RandomCode</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>RandomCode</servlet-name>
- <url-pattern>/randomCode</url-pattern>
- </servlet-mapping>
<servlet> <servlet-name>RandomCode</servlet-name> <servlet-class>com.logcd.servlet.RandomCode</servlet-class> </servlet> <servlet-mapping> <servlet-name>RandomCode</servlet-name> <url-pattern>/randomCode</url-pattern> </servlet-mapping>
3.调用
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <meta http-equiv="pragma" content="no-cache"/>
- <meta http-equiv="cache-control" content="no-cache"/>
- <meta http-equiv="expires" content="0"/>
-
- <iframe src="http://127.0.0.1/js_test/randomCode" id="codeFrame" name="codeFrame" frameborder="no" border="0" marginwidth="0"
- marginheight="0" scrolling="no" allowtransparency="yes" height="35" width="102"></iframe>
- <a href="javascript:void(0);" onclick="refreshCode();">看不清,换一张</a>
- <br>
- <span id="codeImg"><img border=0 src="randomCode"></span>
- <a href="javascript:void(0);" onclick="reloadCode()">看不清,再换一张</a>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><meta http-equiv="pragma" content="no-cache"/><meta http-equiv="cache-control" content="no-cache"/><meta http-equiv="expires" content="0"/><iframe src="http://127.0.0.1/js_test/randomCode" id="codeFrame" name="codeFrame" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes" height="35" width="102"></iframe> <a href="javascript:void(0);" onclick="refreshCode();">看不清,换一张</a><br> <span id="codeImg"><img border=0 src="randomCode"></span><a href="javascript:void(0);" onclick="reloadCode()">看不清,再换一张</a>
- function $(id){
- return document.getElementById(id);
- }
-
-
- function refreshCode(){
- window.frames["codeFrame"].location.reload();
- }
-
-
- function reloadCode(){
- $("codeImg").innerHTML = "<img border=0 src='randomCode'>";
- }
function $(id){ return document.getElementById(id);}/**刷新iframe**/function refreshCode(){ window.frames["codeFrame"].location.reload();}/**替换图片**/function reloadCode(){$("codeImg").innerHTML = "<img border=0 src='randomCode'>";}
<二>、Checked vs UnChecked Exception 任何的异常都是Throwable类,并且在它之下包含两个字类Error / Exception,而Error仅在当在Java虚拟机中发生动态连接失败或其它的定位失败的时候,Java虚拟机抛出一个Error对象。典型的简易程序不捕捉或抛出Errors对象。
Exception中比较重要的就是RuntimeException(运行时异常)-可能在执行方法期间抛出但未被捕捉的 RuntimeException 的任何子类都无需在 throws 子句中进行声明。
除了Error与RuntimeException,其他剩下的异常都是你需要关心的,而这些异常类统称为Checked Exception,至于Error与RuntimeException则被统称为Unchecked Exception。
- Checked exception: 这类异常都是Exception的子类 。异常的向上抛出机制进行处理,假如子类可能产生A异常,那么在父类中也必须throws A异常。可能导致的问题:代码效率低,耦合度过高。C#中就没有使用这种异常机制。
- Unchecked exception: 这类异常都是RuntimeException的子类,虽然RuntimeException同样也是Exception的子类,但是它们是非凡的,它们不能通过client code来试图解决,所以称为Unchecked exception。