Java验证码读取

来源:互联网 发布:化学分析软件 编辑:程序博客网 时间:2024/05/22 01:35

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class ImgDecode{  
    public static byte[] readInputStream(InputStream in) throws Exception{  
        ByteArrayOutputStream out = new ByteArrayOutputStream();  
        byte[] buffer = new byte[1024];  
        int len = 0;  
        while( (len=in.read(buffer)) != -1 ){  
            out.write(buffer, 0, len);  
        }  
        in.close();  
        return out.toByteArray();  
    } 
    
    public static void decodeImage(String imgpath){
    String path = imgpath.substring(0,imgpath.lastIndexOf("\\"));
    System.out.println("path="+path);
   
    String cmd = "cmd /c start .\\decode.bat "+"\""+imgpath+"\"";
    try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();

    }
    
    public static void main(String[] args) throws Exception {  
    URL url = new URL("http://url/checkcode/picCheckCode");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
        conn.setRequestMethod("GET");  
        conn.setConnectTimeout(5 * 1000);  
        InputStream in = conn.getInputStream();  
        byte[] data = readInputStream(in);  
      
        File imageFile = new File("abc.png");  
        FileOutputStream out = new FileOutputStream(imageFile);  
        out.write(data);  
        out.close();  
        System.out.println("imgpath="+imageFile.getAbsolutePath());
       decodeImage(imageFile.getAbsolutePath());
   
       try{
      Thread.sleep(1000);
       }catch(Exception e){
      e.printStackTrace();
       }
       InputStream in1 = new FileInputStream(new File(".\\1.txt"));
       byte[] b = new byte[1024];
       int len = -1;
       while((len=in1.read(b))!=-1){
      in1.read(b);
       }
       if(in!=null){
      try{
      in1.close();
      }catch(Exception e){
      e.printStackTrace();
      }
       }
       String out1 = new String(b);
       System.out.println(out1);
    }
}  

decode.bat:

@echo off
tesseract.exe %1 1 -1
)
exit

0 0