PreviewTextUtils.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. +--------------------------------------------------------------------------
  3. | Mtons [#RELEASE_VERSION#]
  4. | ========================================
  5. | Copyright (c) 2014, 2015 mtons. All Rights Reserved
  6. | http://www.mtons.com
  7. +---------------------------------------------------------------------------
  8. */
  9. package com.nosum.common.util;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.jsoup.Jsoup;
  12. import org.jsoup.nodes.Document;
  13. import org.jsoup.nodes.Element;
  14. import org.jsoup.safety.Whitelist;
  15. /**
  16. * @author: sumbytes
  17. * @date: 2019/8/3 14:57
  18. */
  19. public class PreviewTextUtils {
  20. /**
  21. * 提取纯文本
  22. * @param html 代码
  23. * @return string
  24. */
  25. public static String getText(String html) {
  26. if (html == null) {
  27. return null;
  28. }
  29. return Jsoup.clean(html, Whitelist.none()).trim();
  30. }
  31. /**
  32. * 提取纯文本
  33. * @param html 代码
  34. * @param length 提取文本长度
  35. * @return string
  36. */
  37. public static String getText(String html, int length){
  38. String text = getText(html);
  39. text = StringUtils.abbreviate(text, length);
  40. return text;
  41. }
  42. /**
  43. * 以下标签可以通过 (b, em, i, strong, u. 纯文本)
  44. * @param html 代码
  45. * @return string
  46. */
  47. public static String getSimpleHtml(String html) {
  48. if (html == null) {
  49. return null;
  50. }
  51. return Jsoup.clean(html, Whitelist.simpleText());
  52. }
  53. /**
  54. * 获取文章中的img url
  55. * @param html 代码
  56. * @return string
  57. */
  58. public static String getImgSrc(String html) {
  59. if (html == null) {
  60. return null;
  61. }
  62. Document doc = Jsoup.parseBodyFragment(html);
  63. Element image = doc.select("img").first();
  64. return image == null ? null : image.attr("src");
  65. }
  66. }