使用Java正则表达式匹配出网页图片地址并替换掉

首先说明一下.我是做的邮件发送功能..~~ 图片已经上传到了 服务器, 现在的问题是,发送后的图片不能正常显示,路径不对.. 我是想用Java正则表达式匹配出上传的图片地址并替换掉

 

方法一:

public class TestImg2 {

    private String replaceImgSrc(String content,String replaceHttp){

         String result ="";

//       String patternStr="^.*<img\\s*.*\\s*src=\\\"(.*)\\\"\\s*.*>.*$";

//       String patternStr=".*?<img\\s*.*?\\s*src=\\\"(.*)\\\"\\s*.*?>.*";

//        String patternStr="^.*<img\\s*.*\\s*src=\\\"(.*?)\\\"\\s*.*>.*$";

//       String patternStr="<img(?:.*)src=(\"{1}|\'{1})([^\\[^>]+[gif|jpg|jpeg|bmp|bmp]*)(\"{1}|\'{1})(?:.*)>";

//       String patternStr="<img.*src=(.*?)[^>]*?>src=\"?(.*?)(\"|>|\\s+)";

        

//       String patternStr="(?i)<img[^>]*?src=\"([])\"";

//       String patternStr = "<img\\s+[^>]*?src="((\\w+?:\\/\\/|/)[^"]*?)"[^>]*?>";

 //         String patternStr = "<img\\s+[^>]*?src=\"((\\w+?:?//|\\/|\\w*)[^\"]*?)\"[^>]*?>";

 String patternStr = "<img\\s+[^>]*?src=[\"|\']((\\w+?:?//|\\/|\\w*)[^\"]*?)[\"|\'][^>]*?>";

         Pattern pattern=Pattern.compile(patternStr);

         Matcher matcher = pattern.matcher(content);

          //如果匹配到了img

         System.out.println("matcher.matches() == "+matcher.matches());

         if(matcher.matches()){

           result=content.replaceAll(matcher.group(1),(replaceHttp+matcher.group(1)));

           System.out.println(" result == "+result);

         }else{

             result =content;

         }

         return result;

    }

public static void main(String[] args) {

       TestImg2 ss = new TestImg2();

       String content = "<p><img style=\"width:356px;height:163px;\" title=\"33\" alt=\"33\" src=\"/WorkStation/attached/20110802/20110802131500_758.gif\" width=\"33\" height=\"3\"</p><p><img style=\"width:356px;height:163px;\" title=\"33\" alt=\"33\" src=\"/WorkStation/attached/20110802/20110802131500_758.gif\" width=\"33\" height=\"33\" /></p><p>&nbsp;</p><p><img style=\"width:356px;height:163px;\" title=\"33\" alt=\"33\" src=\"/WorkStation/attached/20110802/20110802131500_758.gif\" width=\"33\" height=\"3\"</p><p><img style=\"width:356px;height:163px;\" title=\"33\" alt=\"33\" src=\"/WorkStation/attached/20110802/20110802131500_758.gif\" width=\"33\" height=\"33\" /></p><p>&nbsp;</p>";

//     String content = "<p><img title=\"33\" alt=\"33\" align=\"left\" src=\"/WorkStation/attached/20110802/20110802173151_741.gif\" width=\"33\" height=\"33\" /></p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;<img title=\"32\" alt=\"32\" src=\"/WorkStation/attached/20110802/20110802173215_520.gif\" width=\"33\" height=\"33\" /></p>" ;

       ss.replaceImgSrc(content, "http://10.10.0.126:8088");

    }

}

 

 

 

 

 

方法二

public class TestImg {

    public static void main(String[] args) {

       String content = "<p><img style=\"width:356px;height:163px;\" title=\"33\" alt=\"33\" src=\"/WorkStation/attached/20110802/20110802131500_758.gif\" width=\"33\" height=\"3\"</p>";

//     String content = "<p><img style=\"width:356px;height:163px;\" title=\"33\" alt=\"33\" src=\"/WorkStation/attached/20110802/20110802131500_758.gif\" width=\"33\" height=\"3\"</p><p><img style=\"width:356px;height:163px;\" title=\"33\" alt=\"33\" src=\"/WorkStation/attached/20110802/20110802131500_758.gif\" width=\"33\" height=\"33\" /></p><p>&nbsp;</p><p><img style=\"width:356px;height:163px;\" title=\"33\" alt=\"33\" src=\"/WorkStation/attached/20110802/20110802131500_758.gif\" width=\"33\" height=\"3\"</p><p><img style=\"width:356px;height:163px;\" title=\"33\" alt=\"33\" src=\"/WorkStation/attached/20110802/20110802131500_758.gif\" width=\"33\" height=\"33\" /></p><p>&nbsp;</p>";

//     content = content.replaceAll("(.*)src=\"(.*)", "$1src=\"http://127.0.0.1:8088$2");

       content = content.replaceAll("(.*?)src=\"(.*?)", "$1src=\"http://127.0.0.1:8088$2");

//     String contents = content.replaceAll("(.*?)src=\"(.*?)", "$1src=\""+path+"$2");

 

       System.out.println(content);

      

//     String str = "<img src=\"/WorkStation/attached/20110729/2011072917.jpg\" width=\"333\" height=\"333\" /><img src=\"/WorkStation/attached/20110729/2011072916.jpg\" width=\"333\" height=\"333\" /><img src=\"/WorkStation/attached/20110729/2011072917.jpg\" width=\"333\" height=\"333\" />";

    }

}

 

    // 获取项目的基路径(用于拼接图片的全路径)

private String basePaths(){

       FacesContext context = FacesContext.getCurrentInstance();

       ExternalContext ec = context.getExternalContext();

       HttpServletRequest request = (HttpServletRequest) ec.getRequest();

      

       String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();

       // System.out.println("request.getRequestURL() == "+request.getRequestURL());

       System.out.println("basePath == "+basePath);

       return basePath;

    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值