maven java web 生成图片验证码

eclipse 安装m2eclipse插件,安装完毕后,新建一个webapp maven项目,项目目录如下:

新建IdentityServlet类:

package com.yeetrack.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class IdentityServlet extends HttpServlet
{

    private static final long serialVersionUID = 4745835569705949574L;

    private static final char[] CHARS = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; //设置验证码字符
    private static Random random = new Random();
    /**
    * 获取四位随机验证码
    * @return
    */
    private static String getRandomString()
    {
        StringBuffer buffer = new StringBuffer();
        for(int i=1;i<=4;i++)
            buffer.append(CHARS[random.nextInt(CHARS.length)]);
        return buffer.toString();
    }
    //随机获取颜色
    private static Color getRandomColor()
    {
        return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
    }
    private static Color getReverseColor(Color c)
    {
        return new Color(255-c.getRed(), 255-c.getGreen(), 255-c.getBlue());
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        // TODO Auto-generated method stub
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("image/jpeg");
        String randomString = getRandomString();
        req.getSession(true).setAttribute("random", randomString);

        int width = 100;
        int height = 30;

        Color color = getRandomColor();
        Color reverse = getReverseColor(color);

        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
        g.setColor(color);
        g.fillRect(0, 0, width, height);
        g.setColor(reverse);
        g.drawString(randomString, 18, 20);
        for(int i=0,n=random.nextInt(100);i<n;i++)
            g.drawRect(random.nextInt(width), random.nextInt(height),1,1);

        ServletOutputStream out = resp.getOutputStream();
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(bi);
        out.flush();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        // TODO Auto-generated method stub
        super.doPost(req, resp);
    }
}

maven的pom.xml如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yeetrack</groupId>
<artifactId>mavenWeb</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mavenWeb Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0.5</version>

</dependency>
<dependency>
    <groupId>org.apache.geronimo.specs</groupId>
    <artifactId>geronimo-servlet_2.5_spec</artifactId>
    <version>1.2</version>
</dependency>


</dependencies>
<build>
<finalName>mavenWeb</finalName>
<plugins>
<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <version>6.1.10</version>
    <configuration>
      <stopPort>9966</stopPort>
      <stopKey>foo</stopKey>  
    </configuration>
</plugin>
</plugins>
</build>
</project>

项目web.xml配置如下:
23.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
    <servlet-name>identityServlet</servlet-name>
    <servlet-class>com.yeetrack.servlet.IdentityServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>identityServlet</servlet-name>
    <url-pattern>/servlet/idetityget</url-pattern>
</servlet-mapping>
</web-app>

maven install,再调用tomcat或者jetty。浏览器中直接访问定义的servlet即可,如http://localhost:8080/mavenWeb/servlet/idetityget

版权声明

本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。

© 空空博客,本文链接:https://www.yeetrack.com/?p=18