当前位置 : 主页 > 编程语言 > c++ >

随机生成验证码和设置响应

来源:互联网 收集:自由互联 发布时间:2021-07-03
gistfile1.txt java随机生成验证码图片1、设置浏览器不要拿缓存response.setHeader("cache-control", "no-cache");response.setHeader("pragma", "no-cache");response.setHeader("expires","-1");2、随机生成验证码BufferedImag
gistfile1.txt
java随机生成验证码图片
1、设置浏览器不要拿缓存response.setHeader("cache-control", "no-cache");
response.setHeader("pragma", "no-cache");
response.setHeader("expires","-1");

2、随机生成验证码
BufferedImage image = new BufferedImage
(width, height, BufferedImage.TYPE_INT_RGB);
Graphics grap = image.getGraphics();
public void setBackground(Graphics grap){      grap.setColor(Color.WHITE);
grap.fillRect(0, 0, width, height);//填满背景色
}
public void setBorder(Graphics grap){ grap.setColor(Color.BLACK);
grap.drawRect(1, 1, width-2, height-2);//画边框
}
public void drawLine(Graphics grap){ grap.setColor(Color.green);
for (int i = 0; i < 6; i++){ grap.drawLine(r.nextInt(200),r.nextInt(50), r.nextInt(200),r.nextInt(50));//画干扰线
        }
}
public void writeChar(Graphics grap){
//常用的汉字的unicode编码
String str = "\u7684\u4e00\u662f\u4e86\u6211\u4e0d\u4eba\u5728\u4ed6\u6709\u8fd9\u4e2a\u4e0a\u4eec\u6765" + "\u5230\u65f6\u5927\u5730\u4e3a\u5b50\u4e2d\u4f60\u8bf4\u751f\u56fd\u5e74\u7740\u5c31\u90a3" + "\u548c\u8981\u5979\u51fa\u4e5f\u5f97\u91cc\u540e\u81ea\u4ee5\u4f1a\u5bb6\u53ef\u4e0b\u800c" + "\u8fc7\u5929\u53bb\u80fd\u5bf9\u5c0f\u591a\u7136\u4e8e\u5fc3\u5b66\u4e48\u4e4b\u90fd\u597d";
grap.setColor(Color.RED);
grap.setFont(new Font("宋体",
Font.ITALIC,30));
int x = 10;
Graphics2D grap2=(Graphics2D) grap;
for(int i=0;i<4;i++){
int temp = r.nextInt(str.length());//随机生成一个字符的下标
int temp2 = r.nextInt()%30;//随机生成一个-30^-30的整数, 旋转角度  grap2.rotate(temp2*Math.PI/180, x, 30);/ /旋转字体
 grap.drawString(str.charAt(temp)+"", x, 30);
//写字
lessent.append(str.charAt(temp)); grap2.rotate(-temp2*Math.PI/180, x, 30);/ /恢复旋转的角度,不影响下个字的旋转角度x+=50;
     }
}
response.setHeader("content-type", "image/jpeg");//告诉浏览数据的类型ImageIO.write(image, "jpg", response.getOutputStream());//输出图片

3、设置标签头
response.setHeader("Content-type","text/html;charset=utf-8");//编码集response.setHeader("content-Encoding", "Gzip");//文件的压缩格式response.setHeader("content-Length", b2.length+"");//告诉浏览器文件大小response.setHeader("refresh", "3;url='/Web/index.html'");
response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(string, "utf-8"));//告诉浏览器下载

2、
meta
 //定时刷新和跳转

 ///设置编码集可以用response的输出流输出这些信息,浏览器会自动识别response.getWriter().write("登入成功hahahah,3秒后将跳转到首页,若没有跳转请点击index.html");out.awrite("".getBytes());
out.write(str.getBytes("utf-8")); this.getServletContext().getRequestDispatcher("/1.jsp").forward(request, response);
网友评论