【问题标题】:Trying to follow URL and then download image尝试关注 URL,然后下载图像
【发布时间】:2012-03-21 04:43:15
【问题描述】:

我正在编写一个 java 程序,它会搜索 twitter,找到某些 instagram 链接,然后将 instagram 图像下载到一个目录。我已经编写了所有代码来进行 twitter 搜索和图像保存,但我遇到的问题是我得到了以下格式的 URL

http://t.co/xxxxxxxx

从 twitter 搜索中,如果关注该搜索,则会解析为 instagram URL,例如

http://instagr.am/p/xxxxxxxxxx/

如何将原始 URL 解析为 instagram URL,然后在该页面上下载适当的图像?

【问题讨论】:

  • 如果您使用java.net.HttpURLConnection,默认情况下应遵循重定向。你试过了吗?

标签: java image url download instagram


【解决方案1】:

Instagram 为此提供了 API 调用。它以 JSON 格式返回您需要的所有内容,只需提供以下短 URL:

http://api.instagram.com/oembed?url=http://instagr.am/p/XXXXXX/

你会得到这样的数据:

{
    provider_url: "http://instagr.am/",
    title: "Rays",
    url: "http://distillery.s3.amazonaws.com/media/2010/10/02/7e4051fdcf1d45ab9bc1fba2582c0c6b_7.jpg",
    author_name: "danrubin",
    media_id: "5382_72",
    author_id: 72,
    height: 612,
    width: 612,
    version: "1.0",
    author_url: "http://instagr.am/",
    provider_name: "Instagram",
    type: "photo"
}

或者如果你想要的只是图片,你可以在 URL 中添加 /media:

http://instagr.am/p/XXXXXX/media

【讨论】:

    【解决方案2】:

    我不确定您的代码做了多少,但您应该能够针对该 t.co 链接发出 HTTP 请求,并在返回的标头中获取 Location 字段,其中包含 instagr.am 链接.

    来自 t.co 链接的标头响应示例:

    HTTP/1.1 301 Moved Permanently
    Date: Wed, 21 Mar 2012 04:48:00 GMT
    Server: hi
    Location: http://instagr.am/p/XXXXXX/
    Cache-Control: private,max-age=300
    Expires: Wed, 21 Mar 2012 04:53:00 GMT
    Connection: close
    Content-Type: text/html; charset=UTF-8
    

    如您所见,Location 字段是您要解析的字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多