示例-下载验证码文件

//https://www.xx.com/captcha.gif?r=1509626035515&type=login

public class App
{
    public static CloseableHttpClient httpclient;
    
    public static HttpGet httpGet;
    
    public static void main(String[] args) throws IOException
    {
        
        // 证书信息,可添加多个
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope("proxyhk.huawei.com", 8080),
                new UsernamePasswordCredentials("username", "password"));
        
        // 创建httpclient
        httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        
        // 请求地址和参数
        httpGet = new HttpGet("https://www.xx.com/captcha.gif?r=1509626035515&type=login");
        
        // 请求信息中设置使用的代理
        HttpHost proxy = new HttpHost("proxyhk.huawei.com", 8080);
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        httpGet.setConfig(config);
        
        try
        {
            //启动多线程
            ExecutorService exec = Executors.newFixedThreadPool(3);
            int step = 10000;
            if (args.length != 0)
            {
                step = Integer.parseInt(args[0]);
            }
            for (int i = 0; i <= 100; i++)
            {
                exec.execute(new LiftOff(httpclient, httpGet, i * step, (i + 1) * step));
            }
            
            exec.shutdown();
        }
        finally
        {
        }
    }
}

class LiftOff implements Runnable
{
    private CloseableHttpClient httpclient;
    
    private HttpGet httpGet;
    
    private int start;
    
    private int end;
    
    LiftOff(CloseableHttpClient httpClient, HttpGet httpGet, int start, int end)
    {
        this.httpclient = httpClient;
        this.httpGet = httpGet;
        this.start = start;
        this.end = end;
    }
    
    @Override
    public void run()
    {
        long startTime = System.currentTimeMillis();
        
        Map<String, byte[]> map = new HashMap<>();
        
        // 执行请求
        HttpResponse response;
        for (int i = start; i <= end; i++)
        {
            try
            {
                response = this.httpclient.execute(httpGet);
                HttpEntity entity = response.getEntity();
                
                byte[] tmp = EntityUtils.toByteArray(entity);
                
                //                System.out.println("读取完成");
                
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                String md5Str = DatatypeConverter.printHexBinary(md5.digest(tmp));
                
                //将md5值和图片内容放入map中
                if (null != map.get(md5Str))
                {
                    md5Str = this.getNextId(map, md5Str);
                }
                
                //将md5和gif内容放入map中
                map.put(md5Str, tmp);
            }
            catch (Exception e)
            {
                System.out.println(e.getStackTrace());
            }
            
        }
        
        System.out.println("开始写入文件");
        
        //文件写入
        for (Entry<String, byte[]> entry : map.entrySet())
        {
            OutputStream out = null;
            try
            {
                out = new FileOutputStream("I:\\下载的知乎验证码\\" + entry.getKey() + ".gif");
                out.write(entry.getValue(), 0, entry.getValue().length);
            }
            catch (Exception e)
            {
                System.out.println("文件写入失败" + entry.getKey());
                System.out.println(e.getMessage());
            }
            finally
            {
                if (null != out)
                {
                    try
                    {
                        out.close();
                    }
                    catch (IOException e)
                    {
                        System.out.println("写入关闭失败:" + entry.getKey());
                    }
                }
            }
        }
        
        System.out.println("总消耗时间:" + (System.currentTimeMillis() - startTime) / 1000.0 + " S");
        
    }
    
    /**
     * 如果MD5值重复,则返回下一个可用的Md5
     * @param map
     * @param md5
     * @return
     * @author z00316474
     * @since  BES V100R001C00
     */
    private String getNextId(Map<String, byte[]> map, String md5)
    {
        
        for (int i = 1; i < Integer.MAX_VALUE; i++)
        {
            if (null == map.get(md5 + "_" + i))
            {
                return (md5 + "_" + i);
            }
        }
        
        return md5;
    }
}
posted @ 2017-11-03 17:21  Desneo  阅读(410)  评论(0编辑  收藏  举报