Java 通過設(shè)置Referer反盜鏈
更新時間:2009年07月09日 14:08:57 作者:
以前寫過通過URLConnection下載圖片等網(wǎng)絡(luò)資源的代碼,不過發(fā)現(xiàn)象新浪等網(wǎng)站,都不允許直接連接,所以增強了代碼,通過模擬仿造referer來實現(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;
/**
* 下載遠程網(wǎng)站的圖片,通過設(shè)置Referer反反盜鏈。
*
* @author JAVA世紀網(wǎng)(java2000.net, laozizhu.com)
*/
public class ImageDownloader {
/**
* 下載文件到指定位置
* @param imgurl 下載連接
* @param f 目標文件
* @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;
}
}
}
復制代碼 代碼如下:
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;
/**
* 下載遠程網(wǎng)站的圖片,通過設(shè)置Referer反反盜鏈。
*
* @author JAVA世紀網(wǎng)(java2000.net, laozizhu.com)
*/
public class ImageDownloader {
/**
* 下載文件到指定位置
* @param imgurl 下載連接
* @param f 目標文件
* @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)頁上嵌入播放器(常用播放器代碼整理)
網(wǎng)頁上嵌入播放器,只要在HTML上添加以上代碼就OK了,下面整理了一些常用的播放器代碼,總有一款適合你,感興趣的朋友可以參考下哈,希望對你有所幫助2013-05-05
JSP頁面的動態(tài)包含和靜態(tài)包含示例及介紹
這篇文章主要介紹了JSP頁面的動態(tài)包含和靜態(tài)包含示例及介紹,本文講解了它們的區(qū)別并給出了相應例子,需要的朋友可以參考下2014-08-08
ResourceBundle類在jsp中的國際化實現(xiàn)方法
下面小編就為大家?guī)硪黄猂esourceBundle類在jsp中的國際化實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07

