首页 > 其他分享 >Response生成验证码

Response生成验证码

时间:2023-01-13 08:55:05浏览次数:23  
标签:20 resp javax image 验证码 生成 new import Response

类代码:

package com.yin;


import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

public class Image extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //浏览器3秒刷新1次
        resp.setHeader("refresh","3");

        //在内存中创建一个图片
        BufferedImage image= new BufferedImage(80,20,BufferedImage.TYPE_INT_RGB);
        //得到图片,一只2D的画笔
        Graphics2D g=(Graphics2D)image.getGraphics();
        //设置图片的背景颜色
        g.setColor(Color.white);
        g.fillRect(0,0,80,20);
        //给图片写数据
        g.setColor(Color.BLUE);
        g.setFont(new Font(null,Font.BOLD,20));
        g.drawString(makeNum(),0,20);
        //告诉浏览器,这个请求用图片的方式打开
        resp.setContentType("image/jpg");
         //网站存在缓存,不让浏览器缓存
        resp.setHeader("Cache-Control","no-cache");
        resp.setHeader("Pragma","no-cache");
        //吧图片写给浏览器
        ImageIO.write(image,"jpg",resp.getOutputStream());
    }

    private String makeNum(){
        Random random=new Random();
        String num=random.nextInt(999999)+"";
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i <7-num.length() ; i++) {
            sb.append("0");
            
        }
        String s=sb.toString()+num;
        return num;
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}

  web.xml中插入

    <servlet>
        <servlet-name>image</servlet-name>
        <servlet-class>com.yin.Image</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>image</servlet-name>
        <url-pattern>/img</url-pattern>
    </servlet-mapping>

  显示结果;

 

 

标签:20,resp,javax,image,验证码,生成,new,import,Response
From: https://www.cnblogs.com/insoon/p/17048508.html

相关文章

  • 图片验证码,发送邮箱,富文本编辑器
    @目录图片验证码1、安装2、设置3、建立表单4、实现邮件发送1、settings.py配置2、发送邮件富文本编辑器1、安装2、settings.py设置3、实现图片验证码1、安装pipinstall......
  • HttpServletResponse下载文件
    简称Response类常见应用向浏览器输出消息下载文件要获取下载文件的路径下载文件的名称设置想办法让浏览器能够支持下载我们需要的东西获取下载文件的输入流创建缓......
  • Gradle生成jar文件的名称与版本问题
    在使用Gradle对SpringBoot进行项目管理时,项目打包的jar文件名称没有版本号,且文件名称后面都加入了plain字样。如何让Gradle打包生成的jar文件符合常见的命名要求。实际上在......
  • 一笔画路径生成(c++版)
    一笔画路径生成(c++)练习图的遍历、回溯新建一个OnePen类;使用setNodeNum()方法设置节点数量;使用setNodeJoin()设置节点连线;执行drawLine()方法即可得出该图的一笔画......
  • rocketMQ中通过消息查看生成者
    在控制台,通过topic或者消息,默认只展示了消费者列表和具体的消费者,没有展示生产者的IP如何查看呢其实这是数据有,但是控制台没有展示后台:消息:输入topic查询最近一个小时......
  • Java项目生成电脑桌面快捷脚本(Mysql数据)
    一、场景说明在项目中,可能有些同事需要查询线上数据库的数据,但又不能泄露密码给他们,手写一个程序方便他们查询。二、Java代码需要引入mysql驱动包:downloads......
  • centos7.9 安装ddddocr验证码识别模块
    正常安装pipinstallddddocr 但是因为会使用国外源,很慢,所以我们使用国内源 首先先安装opencv-python-headless注意要使用小于4.3版本的,否则按照不上,windows忽略这一......
  • python生成应用程序的块截方式
    代码:importos,winshellfromwin32com.clientimportDispatchpath=r"D:/workspace/wwwroot82/pyjiankong/dist/test.lnk"#Pathtobesaved(shortcut)target......
  • C#-生成Excel文件
    引入命名空间:usingMicrosoft.Office.Interop.Excel;usingSystem.Runtime.InteropServices;创建Sheet:varapp=newMicrosoft.Office.Interop.Excel.Applic......
  • dotnet 项目生成自签名证书
    解决dotnet项目浏览器不安全提示     dotnetdev-certs -生成自签名证书,以便在开发中使用HTTPS。dotnetdev-certshttps--cleandotnetdev-certshttp......