//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; } }