Java 通過(guò)設(shè)置Referer反盜鏈
更新時(shí)間:2009年07月09日 14:08:57 作者:
以前寫(xiě)過(guò)通過(guò)URLConnection下載圖片等網(wǎng)絡(luò)資源的代碼,不過(guò)發(fā)現(xiàn)象新浪等網(wǎng)站,都不允許直接連接,所以增強(qiáng)了代碼,通過(guò)模擬仿造referer來(lái)實(shí)現(xiàn)下載。
下面是完整的代碼。
package cn.searchphoto.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;
/**
* 下載遠(yuǎn)程網(wǎng)站的圖片,通過(guò)設(shè)置Referer反反盜鏈。
*
* @author JAVA世紀(jì)網(wǎng)(java2000.net, laozizhu.com)
*/
public class ImageDownloader {
/**
* 下載文件到指定位置
* @param imgurl 下載連接
* @param f 目標(biāo)文件
* @return 成功返回文件,失敗返回null
*/
public static File download(String imgurl, File f) {
try {
URL url = new URL(imgurl);
URLConnection con = url.openConnection();
int index = imgurl.indexOf("/", 10);
con.setRequestProperty("Host", index == -1 ? imgurl.substring(7) : imgurl.substring(7, index));
con.setRequestProperty("Referer", imgurl);
InputStream is = con.getInputStream();
if (con.getContentEncoding() != null && con.getContentEncoding().equalsIgnoreCase("gzip")) {
is = new GZIPInputStream(con.getInputStream());
}
byte[] bs = new byte[1024];
int len = -1;
OutputStream os = new FileOutputStream(f);
try {
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
} finally {
try {
os.close();
} catch (Exception ex) {}
try {
is.close();
} catch (Exception ex) {}
}
return f;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
復(fù)制代碼 代碼如下:
package cn.searchphoto.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;
/**
* 下載遠(yuǎn)程網(wǎng)站的圖片,通過(guò)設(shè)置Referer反反盜鏈。
*
* @author JAVA世紀(jì)網(wǎng)(java2000.net, laozizhu.com)
*/
public class ImageDownloader {
/**
* 下載文件到指定位置
* @param imgurl 下載連接
* @param f 目標(biāo)文件
* @return 成功返回文件,失敗返回null
*/
public static File download(String imgurl, File f) {
try {
URL url = new URL(imgurl);
URLConnection con = url.openConnection();
int index = imgurl.indexOf("/", 10);
con.setRequestProperty("Host", index == -1 ? imgurl.substring(7) : imgurl.substring(7, index));
con.setRequestProperty("Referer", imgurl);
InputStream is = con.getInputStream();
if (con.getContentEncoding() != null && con.getContentEncoding().equalsIgnoreCase("gzip")) {
is = new GZIPInputStream(con.getInputStream());
}
byte[] bs = new byte[1024];
int len = -1;
OutputStream os = new FileOutputStream(f);
try {
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
} finally {
try {
os.close();
} catch (Exception ex) {}
try {
is.close();
} catch (Exception ex) {}
}
return f;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
相關(guān)文章
(jsp/html)網(wǎng)頁(yè)上嵌入播放器(常用播放器代碼整理)
網(wǎng)頁(yè)上嵌入播放器,只要在HTML上添加以上代碼就OK了,下面整理了一些常用的播放器代碼,總有一款適合你,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-05-05
AJAX 自學(xué)練習(xí) 請(qǐng)求與顯示
主要功能輸入 城市代碼觸發(fā)發(fā)送請(qǐng)求最終返回城市名稱。2009-09-09
JSP數(shù)據(jù)分頁(yè)導(dǎo)出下載顯示進(jìn)度條樣式
這篇文章主要介紹了JSP數(shù)據(jù)分頁(yè)導(dǎo)出下載顯示進(jìn)度條樣式的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
JSP頁(yè)面的動(dòng)態(tài)包含和靜態(tài)包含示例及介紹
這篇文章主要介紹了JSP頁(yè)面的動(dòng)態(tài)包含和靜態(tài)包含示例及介紹,本文講解了它們的區(qū)別并給出了相應(yīng)例子,需要的朋友可以參考下2014-08-08
EJB3.0開(kāi)發(fā)之多對(duì)多和一對(duì)一
EJB3.0開(kāi)發(fā)之多對(duì)多和一對(duì)一...2006-10-10
ResourceBundle類在jsp中的國(guó)際化實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇ResourceBundle類在jsp中的國(guó)際化實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07

