index
<form action="<s:url namespace="/" action="login"/>">
<input type="text" name="username" placeholder="用户名">
<input type="text" name="password" placeholder="密码">
<input type="text" name="code" placeholder="验证码">
<img id="img" src="<s:url namespace="/" action="image"/>" alt="">
<input type="submit">
</form>
<script src="/js/jquery-3.3.1.min副本%202.js"></script>
<script>
<%--在网页中, 地址不变 不会发起新的请求--%>
$(function ()
$("#img").click(function () {
$("#img").attr("src", "/image.action?a=" + new Date().getTime());
});
})
</script>
struts
<package name="image" namespace="/" extends="json-default">
<action name="image" class="com.lanou.controller.ImageAction" method="image">
<result name="success" type="stream">
<param name="contentType">
image/jpeg
<!--image/jpg-->
</param>
<param name="inputStream">
inputStream
</param>
<param name="fileName">
fileName
</param>
</result>
</action>
<action name="login" class="com.lanou.controller.ImageAction" method="login">
<result name="success">
/WEB-INF/jsp/show.jsp
</result>
</action>
</package>
image
package com.lanou.util;
import javax.imageio.ImageIO;import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class ImageUtil {
//验证码的随机范围
private static final char[] CHARS = {'0', '1', '2', '3', '4', '5', 'a', 'b', 'c', 'd','f','k','y','w','B','X'
,'Q','T','L','V','M','6','7'};
//验证码的位数
private static final int SIZE = 4;
//图片宽
private static final int WIDTH = 80;
//图片高
private static final int HEIGHT = 35;
//字体大小
private static final int FONT_SIZE = 30;
// 干扰线的数量
private static final int LINE_COUNT = 5;
//返回验证码和图片
public static Map<String, BufferedImage> createCode() {
//画图片
//1. 创建图片
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
//2. 获取图片的画笔
Graphics graphics = image.getGraphics();
//3. 画底色
graphics.setColor(Color.lightGray);
graphics.fillRect(0, 0, WIDTH, HEIGHT);
//4. 画验证码
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < SIZE; i++) {
// 随机颜色
Color color = randomColor();
graphics.setColor(color);
//随机文字
int index = random.nextInt(CHARS.length);
char c = CHARS[index];
sb.append(c);
//设置字体
Font fonts[]=new Font[5];
fonts[0] = new Font("Aharoni", Font.PLAIN, 40);
fonts[1] = new Font("Book Antiqua", Font.PLAIN, 30);
fonts[2] = new Font("Calibri", Font.PLAIN, 25);
fonts[3] = new Font("Lucida Console", Font.PLAIN,35);
fonts[4] = new Font("DilleniaUPC", Font.PLAIN, 20);
// fonts [5] = new Font("宋体", Font.BOLD + Font.ITALIC, FONT_SIZE);
// return fonts[random.nextInt(5)];
graphics.setFont(fonts[random.nextInt(5)]);
//绘制
graphics.drawString(c + "", WIDTH / SIZE * i, 30);
}
//5. 画干扰线
for (int i = 0; i < LINE_COUNT; i++) {
graphics.setColor(randomColor());
int x1 = random.nextInt(WIDTH);
int x2 = random.nextInt(WIDTH);
int y1 = random.nextInt(HEIGHT);
int y2 = random.nextInt(HEIGHT);
graphics.drawLine(x1,x2,y1,y2);
}
Map<String, BufferedImage> map = new HashMap<>();
map.put(sb.toString(), image);
return map;
}
// BufferedImage -> InputStream
public static InputStream imageToStream(BufferedImage image) throws IOException {
//把图片写入流中
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
ImageIO.write(image, "jpg", imageOutputStream);
//把输出流转成输入流
byte[] bytes = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
return inputStream;
}
// 随机颜色
private static Color randomColor(){
Random random = new Random();
int r = random.nextInt(256);
int g = random.nextInt(256);
int b = random.nextInt(256);
Color color = new Color(r,g,b);
return color;
}
}
action
package com.lanou.controller;
import com.lanou.util.ImageUtil;import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
public class ImageAction extends ActionSupport {
private InputStream inputStream;
private String fileName;
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String image() throws IOException {
Map<String, BufferedImage> map = ImageUtil.createCode();
// 获取验证码
String code = map.keySet().iterator().next();
// 存到Session中
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute("code", code);
// 获取图片
BufferedImage image = map.get(code);
inputStream = ImageUtil.imageToStream(image);
return SUCCESS;
}
//登录
private String username;
private String password;
private String code;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String login() {
HttpSession session = ServletActionContext.getRequest().getSession();
String rightCode = (String) session.getAttribute("code");
if (code.equals(rightCode)){
System.out.println("验证码正确");
if (username.equals("admin") && password.equals("123")){
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
}else {
System.out.println("验证码错误");
}
return SUCCESS;
}
}