关于使用oro进行图片替换的问题
最近使用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用法似乎有些问题,替换的时候不能这么做,请问应该如何解决?