如何用JAVA程序读取网页的验证码
网页中的登陆或填写表单是要填写图片类的严整码,
如何使用程序自动读取出来
请高手指教!!!
最好有源代码~
谢谢
[解决办法]
你可以加入我的群28393917
群共享里有个登录模块,是我做的,你可以看看,你的问题就能解决了
[解决办法]
楼主是想做OCR一类的自动识别吧?
如果能自动识别就不叫验证码的
不过一些简单格式的除外:)
[解决办法]
package so.gd.cn.imgmask;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import javax.imageio.*;
public class ImageServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ImageServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy " string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader( "Pragma ", "No-cache ");
response.setHeader( "Cache-Control ", "no-cache ");
response.setDateHeader( "Expires ", 0);
int width=60, height=18;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font( "Times New Roman ",Font.PLAIN,18));
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
g.setColor(getRandColor(160,200));
for (int i=0;i <155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
//String rand = request.getParameter( "rand ");
//rand = rand.substring(0,rand.indexOf( ". "));
String sRand= " ";
for (int i=0;i <4;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*i+6,16);
}
HttpSession session=request.getSession();
session.setAttribute( "rand ",sRand);
g.dispose();
ImageIO.write(image, "JPEG ", response.getOutputStream());
}
private Color getRandColor(int fc,int bc){//???¨·??§???????ú????200,250
Random random = new Random();
if(fc> 255) fc=255;
if(bc> 255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
/*public static void main(String [] args){
ImageServlet im= new ImageServlet();
im.getRand_Str();
}*/
//public String getRand_Str(){//获得随机数
// Random random = new Random();
// Map map = new HashMap();
// for(int i=0;i <26;i++){
// map.put(new Integer(i).toString(),(char)((int) 'A '+i));
// }
//
// for(int i=26;i <52;i++){
// int o=i-26;
// map.put(new Integer(i).toString(),(char)((int) 'a '+o));
// }
// for(int i=52;i <62;i++){
// int o=i-52;
// map.put(new Integer(i).toString(),(char)((int) '0 '+o));
// }
// /* for(int i=0;i <26;i++){
// int o=i;
// System.out.println( map.get(i+ " ")+ "----- "+ map.get((o+26)+ " "));
// }
// for(int i=52;i <62;i++){
// System.out.println( map.get(i+ " "));
// }
// */
//
// int x=random.nextInt(62);
// int y=random.nextInt(62);
// int z=random.nextInt(62);
// System.out.println( "--- "+x+ "--- "+y+ "--- "+z);
// String str = map.get(x+ " ")+ " "+map.get(y+ " ")+map.get(z+ " ");
// System.out.println( "str--> "+str);
// return str;
// /*int g=fc+random.nextInt(bc-fc);
// int b=fc+random.nextInt(bc-fc);*/
// //return new Color(r,g,b);
//
// }
//
//
//}
以上是一个servlet
web.xml 配置:
<servlet>
<display-name> this is to implement the check code </display-name>
<servlet-name> imageServlet </servlet-name>
<servlet-class> so.gd.cn.imgmask.ImageServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> imageServlet </servlet-name>
<url-pattern> /servlet/image </url-pattern>
</servlet-mapping>
页面中的配置:
如index.jsp下面 写以下标签
<img border= "0 " height= "15 " src= " <%=request.getContextPath() %> /servlet/image ">
原理: 当页面请求 <img> 路径后就会去处理这个servlet
然后把文字图片显示在页面上,并且同时保存到session 中
session.setAttribute( "rand ",sRand);
然后你提交表单的时候 只要判断session.getAttribute( "rand ");是不是提交上来的验证码就行了.
[解决办法]
我是新手。我做过这样一个例子,我做了一些带数字的图片作为验证码图片,每张图片对应一个ID,这个ID对应着数据库中的一张表,里面有这张图片上的数字,当你输入图片中的验证码时,就访问数据库,验证一下该ID对应的数字是否是用户输入的数字就可以了。。
学习中。
[解决办法]
汗,lz要得是读图片里面的验证码,怎么回答如何做验证码了...
以前有人试图读取,但是失败了.
只要在图片里面多加干扰线.就不好读取了.