for(int i=0;i<6;i++){
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
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());
}
/**
- Constructor of the object.
*/
public IdentityServlet() {
super();
}
/**
- Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts “destroy” string in log
// Put your code here
}
/**
-
The doGet method of the servlet.
-
This method is called when a form has its tag value method equals to get.
-
@param request the request send by the client to the server
-
@param response the response send by the server to the client
-
@throws ServletException if an error occurred
-
@throws IOException if an error occurred
*/
public 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, n=random.nextInt(100);i<n;i++){
//画最多100个噪音点
g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1); //噪音点在图片上随机选择位置产生
}
ServletOutputStream out=response.getOutputStream();
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out); //转成JPEG格式
encoder.encode(bi); //对图片进行编码
out.flush();
}
/**
-
The doPost method of the servlet.
-
This method is called when a form has its tag value method equals to post.
-
@param request the request send by the client to the server
-
@param response the response send by the server to the client
-
@throws ServletException if an error occurred
-
@throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out.println("");
out.println("")
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
;
out.println("
out.println(" ");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" ");
out.println("");
out.flush();
out.close();
}
/**
-
Initialization of the servlet.
-
@throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
接下来我们建立一个HTML文件来引用这个图片验证码,创建方法是:在项目Iidentity上右键选择New—HTML(Advanced Templatets),在弹出框中设置名字为identity,然后Finish,下面是identity.html的代码: