자바 간단 한 인증 코드 생 성 실현
간단 한 인증 코드 자바 구현--servlet 클래스 생 성 인증 코드 img,session 기록
선생님 께 서 주신 원본 코드 는 여기에 기록 하고 간단 한 인증 코드,자바 그림 을 그립 니 다.
 
 여기에 감사의 뜻 을 표 하고 원본 코드 를 동봉 합 니 다.
// 
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); //    
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//    1000-9999    
 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); //       
 Graphics gra = image.getGraphics();
 Random random = new Random();
 gra.setColor(getRandColor(200, 250)); //     rand  
 gra.fillRect(0, 0, width, height);
 gra.setColor(Color.black); //     
 gra.setFont(mFont);
 //     155    ,                  
 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);
 }
 //          (4   )
 String sRand = "";
 for (int i = 0; i < 4; i++) {
 String rand = String.valueOf(random.nextInt(10));
 sRand += rand;
 //           
 gra.setColor(new Color(20 + random.nextInt(110),
 20 + random.nextInt(110),
 20 + random.nextInt(110))); //           ,          ,        
 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) { //          
 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() {
 }
}이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.