参考 博客: 小曾同志的专栏: https://blog.csdn.net/u010023795/article/details/53509495
工具类
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class WebContentFormatUtil {
private static String sHead = "<html><head><meta name=\"viewport\" content=\"width=device-width, " +
"initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes\" />" +
"<style>img{max-width:100% !important;height:auto !important;}</style>"
+ "<style>body{max-width:100% !important;}</style>" + "</head><body>";
/**
* 图宽度都控制到 100% 小图标会被放到超级大
*/
public static String getNewContent(String htmltext) {
try {
Document doc = Jsoup.parse(htmltext);
Elements elements = doc.getElementsByTag("img");
for (Element element : elements) {
element.attr("max-width", "100%")
.attr("width", "100%")
.attr("height", "auto")
.attr("style", "");
}
return doc.toString();
} catch (Exception e) {
return htmltext;
}
}
/**
* 图片自动适应 过宽缩到100%,正常的不放大
*/
public static String getFinalContent(String htmltext) {
return sHead + htmltext + "</body></html>";
}
}
使用方式:
//用jquery对返回的html进行处理
detailInform.loadDataWithBaseURL(null, getFinalContent(DetailInfo), “text/html”, “utf-8”, null);