关于使用oro进行图片替换的问题

mxlmwl 2007-10-10 03:08:53
最近使用oro库替换图片的时候碰到一个问题。比如网页代码为:
<center><img width="123" height='56' src = " aa/lin04.jpg"></center>
<center><img width="123" height='56' src = "aa/lin04.jpg"></center>
<div id="test">测试中的数据</div>
<a id="DEL_CDrag" href="javascript:void(0);">测试数据</a>
<table width="450" align="center" border="1" style="display:block" class='class1 hehe'>
<thead>
<th colspan="2">标题</th>
</thead>
<center><img width="123" height='56' src = aa/lin05.jpg></center>
<P><img src="/upimg/other3.jpg" width=417>
<br>
</center>
</body>
</html>
而该网页地址假设为http://www.abc.com/test/index.html

我想读取了该网页之后,自动将图片改为绝对地址,像浏览器那样。

我的代码为
String regexp =
"src\\s*=\\s*[\'|\"|\\s]?((.[^\'|\"]*)(\\.)(jpg|gif|png|bmp|jpeg))[\'|\"|\\s|>]?";
PatternCompiler compiler = new Perl5Compiler();
StringBuffer buffer = new StringBuffer();
try {
Pattern pattern =
compiler.compile(regexp, Perl5Compiler.CASE_INSENSITIVE_MASK);
PatternMatcherInput input = new PatternMatcherInput(htmlcode);
PatternMatcher matcher = new Perl5Matcher();

while (matcher.contains(input, pattern)) {
//System.out.println(input.toString()+"\n--------------------\n");
MatchResult result = matcher.getMatch();
String temp = result.group(1);
String singleurl = URLUtil.getRealURL(urlstr, temp);

//htmlcode=StringUtils.replace(input.toString(),temp,singleurl);
//htmlcode = StringUtil.replaceAll(htmlcode, temp, singleurl);
/*htmlcode =
Util.substitute(
matcher,
pattern,
new Perl5Substitution(
singleurl,
Perl5Substitution.INTERPOLATE_ALL),
htmlcode,
Util.SUBSTITUTE_ALL);*/
int a =
Util.substitute(
buffer,
matcher,
pattern,
new Perl5Substitution(
singleurl,
Perl5Substitution.INTERPOLATE_ALL),
input,
Util.SUBSTITUTE_ALL);

System.out.println(
buffer.toString() + "\n--------------------\n");

}
} catch (MalformedPatternException e) {
e.printStackTrace();
}

但是我发现这里的PatternMatcherInput用法似乎有些问题,替换的时候不能这么做,请问应该如何解决?
...全文
116 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧