Many projects need to be rich text to add content, like news ah, tourist attractions and so on, need to use rich text to add data, but how I found on this side two questions
1) How can I get the src image of a rich text?
2) Background upload with a relative path, the front-end display needs to be the most to the path
I'll take a look at the two ways to solve these problems.
1): How to get the rich text picture src? Very simple, just one tool
Public StaticList<string>getimgstr (String htmlstr) {List<String> list =NewArraylist<>(); String img= ""; Pattern P_image; Matcher M_image; //String regex_img = "]*?> "; //Image Link AddressString regex_img = "]*?> "; P_image=pattern.compile (regex_img, pattern.case_insensitive); M_image=P_image.matcher (HTMLSTR); while(M_image.find ()) {//get DataIMG =M_image.group (); //match src data in Matcher m = pattern.compile ("src\\s*=\\s*\"? *?) (\ "|>|\\s+)"). Matcher (IMG); while(M.find ()) {List.add (M.group (1)); } } returnlist; }
You can get the following results
2) Background upload when using a relative path, the front-end display needs is the most to the path, below to see how we dynamically to modify the rich text of the IMG tag src, is also very simple, is a tool can
A jar called Jsoup is needed here, and the MAVEN project can be introduced directly.
<Dependency> <groupId>Org.jsoup</groupId> <Artifactid>Jsoup</Artifactid> <version>1.11.2</version> </Dependency>
Here is the tool class
Public Staticstring Documentbody (String newsbody) {Element doc=jsoup.parsebodyfragment (newsbody). body (); Elements PNGs= Doc.select ("img[src]"); String Httphost= "Http://192.168.0.100"; for(Element element:pngs) {String Imgurl= Element.attr ("src"); if(Imgurl.trim (). StartsWith ("/")) {//will go to match the first character of the src relative path of our rich text picture, please note thatImgurl =httphost +Imgurl; Element.attr ("SRC", Imgurl); } } returnNewsbody =doc.tostring (); }
You can get the following results
Well, that's the solution to these two problems.
Don't like to spray!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Java parsing Rich Text processing img Tags