验证码生成步骤:
controller代码
package com.baizhi.controller; import com.baizhi.utils.VerifyCodeUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; //用户开发 @Controller @RequestMapping("user") public class UserController { @RequestMapping("generateImageCode") public void generateImageCode(HttpSession session, HttpServletResponse response) throws IOException { //1.生成随机字符串 String code = VerifyCodeUtils.generateVerifyCode(4); //2.保存随机字符到session中 session.setAttribute("code",code); //3.将随机字符串生成图片 //4.通过response响应图片 response.setContentType("image/png"); ServletOutputStream os = response.getOutputStream(); VerifyCodeUtils.outputImage(220,80,os,code); } }
其中VerifyCodeUtiles是一个工具类,用于将字符串写入图片中
整体实现一个获得验证码的功能:
效果:
标签:code,VerifyCodeUtils,验证码,session,ems,import,response,第一阶段 From: https://www.cnblogs.com/sxwgzx23/p/18047240