<一>、Java生成驗(yàn)證碼圖片 1.Servlet生成驗(yàn)證碼圖片
- 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 {// 驗(yàn)證碼圖片的寬度。int width = 70;// 驗(yàn)證碼圖片的高度。int height = 30;BufferedImage buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics2D g = buffImg.createGraphics();// 創(chuàng)建一個(gè)隨機(jī)數(shù)生成器類。Random random = new Random();// 設(shè)定圖像背景色(因?yàn)槭亲霰尘埃云?g.setColor(getRandColor(200, 250));g.fillRect(0, 0, width, height);// 創(chuàng)建字體,字體的大小應(yīng)該根據(jù)圖片的高度來定。Font font = new Font("Times New Roman", Font.HANGING_BASELINE, 28);// 設(shè)置字體。g.setFont(font);// 畫邊框。g.setColor(Color.BLACK);g.drawRect(0, 0, width - 1, height - 1);// 隨機(jī)產(chǎn)生155條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到。//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用于保存隨機(jī)產(chǎn)生的驗(yàn)證碼,以便用戶登錄后進(jìn)行驗(yàn)證。StringBuffer randomCode = new StringBuffer();// 設(shè)置默認(rèn)生成4個(gè)驗(yàn)證碼int length = 4;// 設(shè)置備選驗(yàn)證碼:包括"a-z"和數(shù)字"0-9"String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";int size = base.length();// 隨機(jī)產(chǎn)生4位數(shù)字的驗(yàn)證碼。for (int i = 0; i < length; i++) {// 得到隨機(jī)產(chǎn)生的驗(yàn)證碼數(shù)字。int start = random.nextInt(size);String strRand = base.substring(start, start + 1);// 用隨機(jī)產(chǎn)生的顏色將驗(yàn)證碼繪制到圖像中。// 生成隨機(jī)顏色(因?yàn)槭亲銮熬埃云?//g.setColor(getRandColor(1, 100));//調(diào)用函數(shù)出來的顏色相同,可能是因?yàn)榉N子太接近,所以只能直接生成g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));g.drawString(strRand, 15 * i + 6, 24);// 將產(chǎn)生的四個(gè)隨機(jī)數(shù)組合在一起。randomCode.append(strRand);}// 將四位數(shù)字的驗(yàn)證碼保存到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) {// 給定范圍獲得隨機(jī)顏色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.調(diào)用
- <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類,并且在它之下包含兩個(gè)字類Error / Exception,而Error僅在當(dāng)在Java虛擬機(jī)中發(fā)生動(dòng)態(tài)連接失敗或其它的定位失敗的時(shí)候,Java虛擬機(jī)拋出一個(gè)Error對(duì)象。典型的簡(jiǎn)易程序不捕捉或拋出Errors對(duì)象。
Exception中比較重要的就是RuntimeException(運(yùn)行時(shí)異常)-可能在執(zhí)行方法期間拋出但未被捕捉的 RuntimeException 的任何子類都無需在 throws 子句中進(jìn)行聲明。
除了Error與RuntimeException,其他剩下的異常都是你需要關(guān)心的,而這些異常類統(tǒng)稱為Checked Exception,至于Error與RuntimeException則被統(tǒng)稱為Unchecked Exception。
- Checked exception: 這類異常都是Exception的子類 。異常的向上拋出機(jī)制進(jìn)行處理,假如子類可能產(chǎn)生A異常,那么在父類中也必須throws A異常。可能導(dǎo)致的問題:代碼效率低,耦合度過高。C#中就沒有使用這種異常機(jī)制。
- Unchecked exception: 這類異常都是RuntimeException的子類,雖然RuntimeException同樣也是Exception的子類,但是它們是非凡的,它們不能通過client code來試圖解決,所以稱為Unchecked exception。