zoukankan      html  css  js  c++  java
  • 示例-下载验证码文件

    //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;
        }
    }
    
  • 相关阅读:
    BZOJ-1034: [ZJOI2008]泡泡堂BNB (田忌赛马贪心)
    BZOJ-2190: [SDOI2008]仪仗队 (欧拉函数)
    BZOJ-1864: [Zjoi2006]三色二叉树 (julao都说简单的树形DP)
    BZOJ-2657: [Zjoi2012]旅游(journey) (树形DP求最长链)
    BZOJ-2241: [SDOI2011]打地鼠 (模拟+枚举)
    BZOJ-1207: [HNOI2004]打鼹鼠 (LIS类似DP)
    BZOJ-1821: [JSOI2010]Group 部落划分 Group (二分+并查集)
    BZOJ-1218: [HNOI2003]激光炸弹 (前缀和+模拟)
    [SinGuLaRiTy] ZKW线段树
    [SinGuLaRiTy] 字节大小
  • 原文地址:https://www.cnblogs.com/Desneo/p/7779205.html
Copyright © 2011-2022 走看看