Java 通過(guò)設(shè)置Referer反盜鏈
更新時(shí)間:2009年07月09日 14:08:57 作者:
以前寫過(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)文章
SSM框架整合JSP中集成easyui前端ui項(xiàng)目開發(fā)示例詳解
這篇文章主要介紹了SSM框架JSP中集成easyui前端ui項(xiàng)目開發(fā)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10jsp簡(jiǎn)單實(shí)現(xiàn)頁(yè)面之間共享信息的方法
這篇文章主要介紹了jsp簡(jiǎn)單實(shí)現(xiàn)頁(yè)面之間共享信息的方法,以實(shí)例形式簡(jiǎn)單分析了JSP頁(yè)面之間共享參數(shù)的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09詳解Spring data 定義默認(rèn)時(shí)間與日期的實(shí)例
這篇文章主要介紹了詳解Spring data 定義默認(rèn)時(shí)間與日期的實(shí)例的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08Eclipse XSD 生成枚舉類型的Schema的實(shí)例詳解
這篇文章主要介紹了Eclipse XSD 生成枚舉類型的Schema的實(shí)例詳解的相關(guān)資料,希望通過(guò)本能幫助到大家,需要的朋友可以參考下2017-09-09JSP運(yùn)行原理和九大隱式對(duì)象說(shuō)明
JSP運(yùn)行原理和九大隱式對(duì)象說(shuō)明,需要的朋友可以參考一下2013-03-03SSH整合中 hibernate托管給Spring得到SessionFactory
Spring文件中的 SessionFactory中 加入為了能得到同一個(gè)Session2009-06-06