zoukankan      html  css  js  c++  java
  • 抓取RSS源中链接图片问题REDIRECT_LOCATIONS 悟寰轩

    抓取链接里的图片时通常和rss原理的url不是对应的,中间可能会有跳转,用到了如下方法:

       private String getWebRealPath(String src, String link) {
    try {
    if (src.startsWith("/")) {
    link = getRealLink(link);
    src = link.substring(0, link.indexOf("/", 7)) + src;
    }
    if (src.startsWith("./")) {
    link = getRealLink(link);
    src = link.substring(0, link.lastIndexOf("/")) + src.substring(1);
    }
    if (src.startsWith("../")) {
    link = getRealLink(link);
    String[] str = link.split("/");
    int len = src.split("\\.\\./").length;
    String s = "";
    if (link.split("/").length != 3) {
    for (int i = 0; i < str.length - len; i++) {
    s += str[i] + "/";
    }
    src = s + src.replaceAll("\\.\\./", "");
    } else {
    src = link + "/" + src.replaceAll("\\.\\./", "");
    }
    }
    } catch (Exception e) {
    return src;
    }
    return src;
    }
    private String getRealLink(String link) {
    try {
    HttpContext httpContext = new BasicHttpContext();
    HttpUtil.get(link, "iso-8859-1", httpContext);
    RedirectLocations redirectLocations = (RedirectLocations) httpContext
    .getAttribute(DefaultRedirectStrategy.REDIRECT_LOCATIONS);
    if (redirectLocations != null) {
    List uriList = redirectLocations.getAll();
    URI uri = uriList.get(uriList.size() - 1);
    link = uri.toString();
    }
    } catch (Exception e) {
    return link;
    }
    return link;
    }

     

  • 相关阅读:
    jquery 监听input的value值改变
    Win10家庭版共享打印机启用Guest账户
    js数组操作大全(pop,push,unshift,splice,shift方法)
    EF Core索引
    工作经验(C++篇)
    FFmpeg编译i386 OSX 脚本
    xcrun -sdk 选择
    Unity GL 画圆
    OpenGL ES无法获取贴图数据原因
    Unity在Android和iOS中如何调用Native API
  • 原文地址:https://www.cnblogs.com/sunxucool/p/2800014.html
Copyright © 2011-2022 走看看