Java parsing Rich Text processing img Tags

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.