Esempio n. 1
0
  /**
   * 取出静态资源文件
   *
   * @param page
   * @param html
   * @return String
   * @author wx
   */
  private String getStaticResources(HtmlPage page, String html) {

    // js文件
    List<HtmlElement> scripts = page.getElementsByTagName("script");
    for (HtmlElement script : scripts) {
      if (script.hasAttribute("src")) {
        String src = script.getAttribute("src");
        if (StringUtil.notEmpty(src) && staticModels.get(src) == null) {
          ScriptModel model = new ScriptModel(src);
          html = html.replace(src, model.getFilePath());
          staticModels.put(src, model);
        }
      }
    }

    // css资源
    List<HtmlElement> stylesheets = page.getElementsByTagName("link");
    for (HtmlElement style : stylesheets) {
      if (style.hasAttribute("type")
          && style.getAttribute("type").equalsIgnoreCase("text/css")
          && style.hasAttribute("href")) {
        String src = style.getAttribute("href");
        if (StringUtil.notEmpty(src) && staticModels.get(src) == null) {
          CssModel model = new CssModel(src);
          html = html.replace(src, model.getFilePath());
          staticModels.put(src, model);
        }
      }
    }

    // 图片资源
    List<HtmlElement> images = page.getElementsByTagName("img");
    for (HtmlElement image : images) {
      if (image.hasAttribute("src")) {
        String src = image.getAttribute("src");
        if (StringUtil.notEmpty(src) && staticModels.get(src) == null) {
          ImageModel model = new ImageModel(src);
          html = html.replace(src, model.getFilePath());
          staticModels.put(src, model);
        }
      }
    }
    return html;
  }
Esempio n. 2
0
  /**
   * 格式化静态资源链接地址,改为外链
   *
   * @param page
   * @param html
   * @return String
   * @author wx
   */
  private String changeStaticResources(HtmlPage page, String html, String pageUrl) {
    List<String> urlset = new ArrayList<String>();
    // js文件
    List<HtmlElement> scripts = page.getElementsByTagName("script");
    for (HtmlElement script : scripts) {
      if (script.hasAttribute("src")) {
        String src = script.getAttribute("src");
        if (StringUtil.notEmpty(src) && staticModels.get(src) == null && !urlset.contains(src)) {
          String newSrc = HttpUtil.getResourceStaticUrl(pageUrl, src);
          html = html.replace(src, newSrc);
          urlset.add(src);
        }
      }
    }

    // css资源
    List<HtmlElement> stylesheets = page.getElementsByTagName("link");
    for (HtmlElement style : stylesheets) {
      if (style.hasAttribute("type")
          && style.getAttribute("type").equalsIgnoreCase("text/css")
          && style.hasAttribute("href")) {
        String src = style.getAttribute("href");
        if (StringUtil.notEmpty(src) && staticModels.get(src) == null && !urlset.contains(src)) {
          String newSrc = HttpUtil.getResourceStaticUrl(pageUrl, src);
          html = html.replace(src, newSrc);
          urlset.add(src);
        }
      }
    }

    // 图片资源
    List<HtmlElement> images = page.getElementsByTagName("img");
    for (HtmlElement image : images) {
      if (image.hasAttribute("src")) {
        String src = image.getAttribute("src");
        if (StringUtil.notEmpty(src) && staticModels.get(src) == null && !urlset.contains(src)) {
          String newSrc = HttpUtil.getResourceStaticUrl(pageUrl, src);
          html = html.replace(src, newSrc);
          urlset.add(src);
        }
      }
    }
    return html;
  }