java 如何下载受热链接保护的图片?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2011083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:54:37  来源:igfitidea点击:

How to download hotlink protected images?

javaimagehotlinking

提问by Yatendra Goel

I want to download images from other websites that are hotlink protected. I don't want to link those images to my website. I just wanted to download them.

我想从其他受热链接保护的网站下载图像。我不想将这些图像链接到我的网站。我只是想下载它们。

回答by Aziz

The usual hotlink-protection methodchecks if the "Referrer" HTTP Headermatches the domain name of the original website.

通常的热链保护方法检查是否“推荐人” HTTP标头相匹配的原始网站的域名。

You can easily bypass that by setting that header manually to point to a page in the website.

您可以通过手动设置该标题以指向网站中的页面来轻松绕过它。

回答by Gattster

You need to pass the referrer http header. You can do this with wget on most unix systems as follows:

您需要传递引用 http 标头。您可以在大多数 unix 系统上使用 wget 执行此操作,如下所示:

wget --referer=http://www.google.com/ http://www.google.com/intl/en_ALL/images/logo.gif

Here a raw way to do it so you see exactly what is going on:

这是一种原始的方法,因此您可以准确了解正在发生的事情:

telnet google.com 80
GET /intl/en_ALL/images/logo.gif HTTP/1.1
REFERER: http://www.google.com/
HOST: www.google.com

回答by Birdman

You can download hotlink protected images by using the following code:

您可以使用以下代码下载受热链接保护的图像:

URL url = new URL("http://www.somesite.com/picture.jpg");

URLConnection urlCon = url.openConnection();
urlConn.setRequestProperty("Referer", "http://www.somesite.com");
urlConn.connect();

InputStream urlStream = urlCon.getInputStream();

Image image = ImageIO.read(urlStream);

回答by Dan Ross

The Postman extension for Chrome lets you make custom http requests. I found a hotlink-blocked image, copied it's url and entered it into Postman to GET it.

Chrome 的 Postman 扩展程序可让您自定义 http 请求。我找到了一个阻止热链接的图像,复制了它的 url 并将其输入 Postman 以获取它。