一,图片下载方法
如果在源文件中,验证码是img形式,就非常好下载处理了。这里就不在陈述了,目前好多验证码都是数据流形式,需要编程处理,首先,要获取验证码的地址,例如:地址为http://aaa.bbb.ccc/code
public static void GetURL() throws ClientProtocolException, IOException { // 1,获取图片 @SuppressWarnings("deprecation") HttpClient httpclient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://aaa.bbb.ccc/code");//图片的URL httpPost.setHeader(CommonConst.UserAgent, CommonConst.HttpAgent); httpPost.setHeader("Cache-Control", "max-age=0"); httpPost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); httpPost.setHeader("Accept-Encoding", "gzip,deflate,sdch"); httpPost.setHeader("Accept-Language", "en-US,en;q=0.8"); httpPost.setHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3"); httpPost.setHeader("Accept-Encoding", "gzip,deflate,sdch"); HttpResponse response = httpclient.execute(httpPost); HttpEntity entity = response.getEntity(); byte[] imgArray = new byte[200 * 60];//下载图片后,看图片的大小 if (entity != null) { java.io.InputStream instreams = entity.getContent(); instreams.read(imgArray); httpPost.abort(); } // 2,保存图片 mWriterPicture("E:\\testc\\1原始验证码\\a.jpg", imgArray); }
// 写入图片 public static void mWriterPicture(String newFileName, byte[] b)throws IOException { try { File file = new File(newFileName); FileOutputStream fStream = new FileOutputStream(file); fStream.write(b); fStream.close(); System.out.println("图片创建成功 " + b.length); } catch (Exception e) { // TODO: handle exception } }
时间: 2024-10-10 20:13:55