lancijk 发表于 2013-2-3 11:17:34

java从网页中提取图片地址

import java.util.regex.Matcher;import java.util.regex.Pattern;//1:这个是拿到一个字符取得里面的图像地址返回一个List public static List<String> getImgStr(String htmlStr){               String img="";               Pattern p_image;               Matcher m_image;               List<String> pics = new ArrayList<String>();                  String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址               p_image = Pattern.compile                     (regEx_img,Pattern.CASE_INSENSITIVE);            m_image = p_image.matcher(htmlStr);            while(m_image.find()){                   img = img + "," + m_image.group();                   Matcher m= Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src                while(m.find()){                   pics.add(m.group(1));                }            }                  return pics;         }   //2:下面是获得第一个地址存入到数据库    public String saveOrUpdate()   {      try{      if(infoId !=null){      infoTpicnew = infoTpicnewService.getInfoTpicnew(infoId);      }else{            infoTpicnew.setInfoDatetime(new Date());      }      infoTpicnew.setInfoContent(infoContent);      List list =getImgStr(infoContent);      infoTpicnew.setInfoPicname((String) list.get(0));         infoTpicnewService.saveOrUpdateInfoTpicnew(infoTpicnew);            return SUCCESS;      } catch (Exception e) {            e.printStackTrace();            return INPUT;      }    }本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lancijk/archive/2010/03/17/5389394.aspx
页: [1]
查看完整版本: java从网页中提取图片地址