java中怎么生成驗(yàn)證碼-創(chuàng)新互聯(lián)

java中怎么生成驗(yàn)證碼,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

創(chuàng)新互聯(lián)自2013年起,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元柯坪做網(wǎng)站,已為上家服務(wù),為柯坪各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575

// package app61;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.image.*;public class VerifyCode extends HttpServlet {private Font mFont = new Font("Times New Roman", Font.PLAIN, 18); //設(shè)置字體public void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//取得一個(gè)1000-9999的隨機(jī)數(shù) HttpSession session = request.getSession(true); response.setContentType("image/gif"); response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); int width = 60, height = 20; ServletOutputStream out = response.getOutputStream(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //設(shè)置圖片大小的 Graphics gra = image.getGraphics(); Random random = new Random(); gra.setColor(getRandColor(200, 250)); //設(shè)置背景色rand墊底 gra.fillRect(0, 0, width, height); gra.setColor(Color.black); //設(shè)置字體色 gra.setFont(mFont); // 隨機(jī)產(chǎn)生155條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到 gra.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);  gra.drawLine(x, y, x + xl, y + yl); } // 取隨機(jī)產(chǎn)生的認(rèn)證碼(4位數(shù)字) String sRand = ""; for (int i = 0; i < 4; i++) {  String rand = String.valueOf(random.nextInt(10));  sRand += rand;  // 將認(rèn)證碼顯示到圖象中  gra.setColor(new Color(20 + random.nextInt(110),  20 + random.nextInt(110),  20 + random.nextInt(110))); //調(diào)用函數(shù)出來(lái)的顏色相同,可能是因?yàn)榉N子太接近,所以只能直接生成  gra.drawString(rand, 13 * i + 6, 16); } session.setAttribute("rand",sRand); // session.setAttribute("getImg", sRand); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close();}static 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);} //Clean up resources public void destroy() {  }}

關(guān)于java中怎么生成驗(yàn)證碼問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

名稱欄目:java中怎么生成驗(yàn)證碼-創(chuàng)新互聯(lián)
鏈接地址:http://bm7419.com/article4/hdpie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)商城網(wǎng)站、網(wǎng)站維護(hù)標(biāo)簽優(yōu)化、網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司