private StringgetNewContent(String htmltext){
Document doc= Jsoup.parse(htmltext);
Elements elements=doc.getElementsByTag("img");
for (Element element : elements) {
element.attr("width","100%").attr("height","auto").attr("max-width","100%");
}
Elements esd = doc.select("[style]");
Iterator iterator = esd.iterator();
while (iterator.hasNext())
{
Element etemp = iterator.next();
String styleStr = etemp.attr("style");
etemp.removeAttr("style");
etemp.attr("style", cssStr(styleStr));
}
// Elements elementstyle=doc.getElementsByTag("style");
// for (Element element : elementstyle) {
// element.attr("width","100%");
// }
return doc.toString();
}
public StringcssStr(String str) {
if (!str.contains("width")) {
return str;
}
String s1 = str.substring(0, str.indexOf("width"));
String s2 = str.substring(str.indexOf("width"), str.length());
String s3 = s2.substring(s2.indexOf(";"));
return s1 +"width:100%" + s3;
}
部分代码思路引用了网友的