验证码
程序员文章站
2022-03-04 22:02:04
...
import java.awt.Color; import java.awt.Font; import java.awt.GradientPaint; import java.awt.Graphics2D; import java.awt.GraphicsEnvironment; import java.awt.Point; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /** * 验证图片的返回 * * @author Vernon.Chen * */ public class VerifyCodeAction extends ActionSupport { private static int LENGTH = 4; // 控制验证码的长度 /** * 生成图片 * * @param code * 验证码组合 * @return */ private BufferedImage getImage(String code) { List<String> fonts = new ArrayList<String>(); GraphicsEnvironment.getLocalGraphicsEnvironment().preferLocaleFonts(); String[] names = GraphicsEnvironment.getLocalGraphicsEnvironment() .getAvailableFontFamilyNames(Locale.ENGLISH); for (String s : names) { char c = s.charAt(0); if (Character.isLowerCase(c) || Character.isUpperCase(c)) { } else { fonts.add(s); } } if (fonts.size() == 0) { names = GraphicsEnvironment.getLocalGraphicsEnvironment() .getAvailableFontFamilyNames(Locale.CHINA); for (String s : names) { char c = s.charAt(0); if (Character.isLowerCase(c) || Character.isUpperCase(c)) { } else { fonts.add(s); } } } int w = 25 * LENGTH; int h = 30; BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); try { Graphics2D g = image.createGraphics(); char[] use = new char[LENGTH]; // g.setColor(new Color(240,240,240)); // g.setBackground(new Color((int) (Math.random() * 256)); g.fillRect(0, 0, w, h); for (int i = 0; i < LENGTH; i++) { Point p = new Point( 1 + (i * ((int) (Math.random() * 10) + (int) w / 5)), h - 5); int size = 0; int[] sizes = new int[15]; for (int j = 0; j < 15; j++) { sizes[j] = 15 + j; } size = sizes[(int) (Math.random() * sizes.length)]; int face = 0; if (Math.random() * 10 > 5) { face = Font.BOLD; } else { face = Font.ITALIC; } use[i] = code.charAt(i); g.setPaint(new GradientPaint(p.x, p.y, new Color((int) (Math .random() * 256), 0, (int) (Math.random() * 256)), p.x, p.y - size, new Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math .random() * 256)))); if (fonts.size() > 0) { g.setFont(new Font(fonts.get((int) (Math.random() * fonts .size())), face, size)); } else { g.setFont(null); } g.drawString("" + use[i], p.x, p.y); } g.setPaint(null); for (int i = 0; i < 6; i++) { g.setColor(new Color((int) (Math.random() * 0x00FFFFFFF))); g.drawLine((int) (Math.random() * w), (int) (Math.random() * h), (int) (Math.random() * w), (int) (Math.random() * h)); } g.dispose(); } catch (Exception e) { e.printStackTrace(); } image.flush(); return image; } public String verifyCode() throws Exception { // 生成随机类 Random random = new Random(); // 取随机产生的认证码(LENGTH位数字) String sRand = ""; for (int i = 0; i < LENGTH; i++) { String rand = String.valueOf(random.nextInt(10)); sRand += rand; } BufferedImage image = getImage(sRand); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("image/jpg"); response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); ServletOutputStream out = response.getOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "JPEG", baos); byte[] buffer = baos.toByteArray(); response.setContentLength(buffer.length); out.write(buffer); out.close(); return null; } }验证码测试:<br/> <img alt="验证码" src="/wenda/verifyCodeAction!verifyCode.action" />
上一篇: php如何实现ajax请求