## 在Sevlet中使用
```
public class ValidateColor extends HttpServlet {
private static final long serialVersionUID = 1L;
//session的name值 。。。CHECK_CODE_KEY
public static final String CHECK_CODE_KEY = "CHECK_CODE_KEY";
//設置驗證圖片的寬度, 高度, 驗證碼的個數
private int width = 152;
private int height = 40;
//驗證碼中的代碼的個數 。ASDsC ABsE
private int codeCount = 4;
//驗證碼字體的高度
private int fontHeight = 4;
//驗證碼中的單個字符基線. 即:驗證碼中的單個字符位于驗證碼圖形左上角的 (codeX, codeY) 位置處
private int codeX = 0;
private int codeY = 0;
//驗證碼由哪些字符組成
char [] codeSequence = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz23456789".toCharArray();
//初始化驗證碼圖形屬性
public void init(){
fontHeight = height - 2;
codeX = width / (codeCount + 2);
codeY = height - 4;
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//定義一個類型為 BufferedImage.TYPE_INT_BGR 類型的圖像緩存
BufferedImage buffImg = null;
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
//在 buffImg 中創建一個 Graphics2D 圖像
Graphics2D graphics = null;
graphics = buffImg.createGraphics();
//設置一個顏色, 使 Graphics2D 對象的后續圖形使用這個顏色
graphics.setColor(Color.WHITE);
//填充一個指定的矩形: x - 要填充矩形的 x 坐標; y - 要填充矩形的 y 坐標; width - 要填充矩形的寬度; height - 要填充矩形的高度
graphics.fillRect(0, 0, width, height);
//創建一個 Font 對象: name - 字體名稱; style - Font 的樣式常量; size - Font 的點大小
Font font = null;
font = new Font("", Font.BOLD, fontHeight);
//使 Graphics2D 對象的后續圖形使用此字體
graphics.setFont(font);
graphics.setColor(Color.BLACK);
//繪制指定矩形的邊框, 繪制出的矩形將比構件寬一個也高一個像素
graphics.drawRect(0, 0, width - 1, height - 1);
//隨機產生 15 條干擾線, 使圖像中的認證碼不易被其它程序探測到
Random random = null;
random = new Random();
graphics.setColor(Color.GREEN);
for(int i = 0; i < 55; i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(20);
int y1 = random.nextInt(20);
graphics.drawLine(x, y, x + x1, y + y1);
}
//創建 randomCode 對象, 用于保存隨機產生的驗證碼, 以便用戶登錄后進行驗證
StringBuffer randomCode;
randomCode = new StringBuffer();
for(int i = 0; i < codeCount; i++){
//得到隨機產生的驗證碼數字
String strRand = null;
strRand = String.valueOf(codeSequence[random.nextInt(36)]);
//把正在產生的隨機字符放入到 StringBuffer 中
randomCode.append(strRand);
//用隨機產生的顏色將驗證碼繪制到圖像中
graphics.setColor(Color.BLUE);
graphics.drawString(strRand, (i + 1)* codeX, codeY);
}
//再把存放有所有隨機字符的 StringBuffer 對應的字符串放入到 HttpSession 中
request.getSession().setAttribute(CHECK_CODE_KEY, randomCode.toString());
//禁止圖像緩存
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
//將圖像輸出到輸出流中
ServletOutputStream sos = null;
sos = response.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}
}
```
## 在springMVC中使用
https://www.cnblogs.com/moonlightL/p/7282469.html
- 第一章 java SE
- 1.1數據類型
- 1.2 流程控制語句
- 1.3 方法
- 1.4 面向對象三特性
- 1.5 對象數組與集合
- 1.6 數組和集合操作工具類
- 1.7 可變參數
- 1.8 String
- 1.9 StringBuilder
- 1.10 final&&finally&&finalize
- 1.11 抽象類與接口
- 1.12 基本數據類型的包裝類
- 1.13 泛型
- 1.14 內部類
- 1.15 throw & throws & try catch
- 1.16 線程
- 1.17 BeanUtils
- 1.18 java反射
- 1.19 序列化和反序列化
- 1.20 IO輸入輸出流
- 1.21 File
- 1.22 RandomAccessFile
- 1.23 第三方工具CommonsIO
- 1.24 java網絡傳輸
- 第二章 java EE
- 2.1 maven的配置
- 2.2 Cookie
- 2.3 EL表達式 JSTL
- 2.4 驗證相關
- 2.4.1 驗證碼
- 2.5 防重復提交
- 2.6 activeMq的使用
- 2.7 jtl的使用
- 2.8 Upload上傳文件
- 第三章 Spring相關
- 3.1 IOC/DI
- bean的生命周期
- bean的配置
- 3.2 Spring Aop
- 3.3 Spring Jdbc
- 3.4 事物相關
- 事物
- 事物的使用
- 3.5 MBG使用
- 第四章 解決問題方法
- 4.1 List轉換為Map
- 4.2 結果返回類
- 4.3 HSSF的使用
- 第五章 排序
- 5.1 冒泡排序
- 5.2 選擇排序
- 5.3 快速排序