📄 imageservlet.java
字号:
package com.pet.util;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.Color;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Font;
public class ImageServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private static final long serialVersionUID = 8109503217290526554L;
private static final Font FONT = new Font("Times New Roman", Font.ITALIC,
18);
//new Font("Times New Roman",Font.ITALIC,18)
private int defaultWidth; // 图片宽度
private int defaultHeight; // 图片高度
private int defaultLength; // 生成随机字母长度
private String defaultKey = "checkRand"; // 保存在会话中的key值
public void init() throws ServletException {
super.init();
String key = this.getInitParameter("key");
if (key != null && key.equals("")) {
defaultKey = key;
}
String width = this.getInitParameter("width");
try {
defaultWidth = Integer.parseInt(width);
} catch (NumberFormatException e) {
defaultWidth = 60;
}
String height = this.getInitParameter("height");
try {
defaultHeight = Integer.parseInt(height);
} catch (NumberFormatException e) {
defaultHeight = 18;
}
String length = this.getInitParameter("length");
try {
defaultLength = Integer.parseInt(length);
} catch (NumberFormatException e) {
defaultLength = 4;
}
}
private int getImageWidth(String widtha) {
int width = 0;
try {
width = Integer.parseInt(widtha);
} catch (NumberFormatException e) {
width = defaultWidth;
}
return width;
}
private int getImageHeight(String heighta) {
int height = 0;
try {
height = Integer.parseInt(heighta);
} catch (NumberFormatException e) {
height = defaultHeight;
}
return height;
}
private int getLength(String lengtha) {
int length = 0;
try {
length = Integer.parseInt(lengtha);
} catch (NumberFormatException e) {
length = defaultLength;
}
return length;
}
private 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);
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setHeader("Pragma", "No-cache"); // 设置响应的文件头
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg"); // 表明生成的响应是图片,而非JSP页面
String key = request.getParameter("key");
if (key == null || key.equals("")) {
key = defaultKey;
}
int width = getImageWidth(request.getParameter("width"));
int height = getImageHeight(request.getParameter("height"));
int length = getLength(request.getParameter("length"));
// 在内存中生成图片
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 开始制作图像
Graphics graphics = image.getGraphics();
Random random = new Random();
graphics.setColor(getRandColor(200, 250));
graphics.fillRect(1, 1, width - 1, height - 1);
graphics.setColor(new Color(102, 102, 102));
graphics.drawRect(0, 0, width - 1, height - 1);
graphics.setFont(FONT);
graphics.setColor(getRandColor(160, 200));
// 画随机线条
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int x1 = random.nextInt(6) + 1;
int y1 = random.nextInt(12) + 1;
graphics.drawLine(x, y, x + x1, y + y1);
}
// 从另一个方向画随机线条
for (int i = 0; i < 70; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int x1 = random.nextInt(12) + 1;
int y1 = random.nextInt(6) + 1;
graphics.drawLine(x, y, x - x1, y - y1);
}
// 生成随机数,并将随机数转换成字母
String sRand = "";
for (int i = 0; i < length; i++) {
int itmp = random.nextInt(26) + 65;
char ctmp = (char) itmp;
sRand += String.valueOf(ctmp);
graphics.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
graphics.drawString(String.valueOf(ctmp), 12 * i + 3, 16);
}
//如果布尔值为true且当前没有与请求关联的会话,则使用getSession(boolean value)会创建一个新会话
HttpSession session = request.getSession(true);
session.setAttribute("key", sRand); //将验证码放入会话当中
graphics.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream()); // 输出图片到Servlet响应
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -