AuthImg.java
资源名称:Myblog.rar [点击查看]
上传用户:wlfwy2004
上传日期:2016-12-12
资源大小:33978k
文件大小:2k
源码类别:
Jsp/Servlet
开发平台:
Java
- package com.opensource.blog.web.servlet;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import com.laoer.comm.util.Util;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- public class AuthImg
- extends HttpServlet {
- private static final String CONTENT_TYPE = "text/html; charset=GBK";
- private Font mFont = new Font("Times New Roman", Font.PLAIN, 12);
- //Initialize global variables
- public void init() throws ServletException {
- }
- //Process the HTTP Get request
- public void doGet(HttpServletRequest request, HttpServletResponse response) throws
- ServletException, IOException {
- String s = Util.genNumPassword(4);
- HttpSession session = request.getSession();
- session.setAttribute("authcode", s);
- response.setContentType("image/jpeg");
- ServletOutputStream out = response.getOutputStream();
- BufferedImage image = new BufferedImage(35, 14, BufferedImage.TYPE_INT_RGB);
- Graphics gra = image.getGraphics();
- gra.setColor(Color.yellow);
- gra.fillRect(1, 1, 33, 12);
- gra.setColor(Color.black);
- gra.setFont(mFont);
- char c;
- for (int i = 0; i < 4; i++) {
- c = s.charAt(i);
- gra.drawString(String.valueOf(c), i * 7 + 4, 11);
- }
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(image);
- out.close();
- }
- //Process the HTTP Post request
- public void doPost(HttpServletRequest request, HttpServletResponse response) throws
- ServletException, IOException {
- doGet(request, response);
- }
- //Clean up resources
- public void destroy() {
- }
- }