最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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;
}
}
}

相關(guān)文章

最新評論

湘潭市| 萍乡市| 筠连县| 苍梧县| 呈贡县| 武城县| 桑植县| 齐齐哈尔市| 信丰县| 泰安市| 泾川县| 响水县| 什邡市| 突泉县| 江永县| 湾仔区| 新民市| 仙游县| 百色市| 中山市| 仁怀市| 新昌县| 惠安县| 将乐县| 东海县| 三台县| 清远市| 淄博市| 杭锦旗| 图片| 顺昌县| 巨野县| 富锦市| 北辰区| 吴旗县| 曲周县| 南皮县| 云龙县| 松潘县| 新绛县| 恭城|