昨天在网上想找一个下载jandan 里无聊图的工具。没有找到java版的。就写了一个简单的下载无聊图的代码。

public class jandan {
// 煎蛋无聊图的地址 也可以换成是妹子图
// 妹子图URL:http://jandan/ooxx
public String jandan="http://jandan/pic";
// 煎蛋页码url后缀
public String prfix ="/page-@#comments";
// 图片保存路径,必须是存在的路径
public String downLoadPath="d:\\jandan\\gaoxiao\\";
// 无聊图开始页码
public int currentPage = 855;
// 图片的url
private ArrayList<String> result = new ArrayList<String>();

private void analyse(int page){
try {
URL url = new URL(jandan+prfix.replace("@",page+""));
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
InputStream in = null;
in = url.openStream();
String content = pipe(in,"utf-8");
String[] imgs = content.split("<img");
ArrayList<String> tmp = new ArrayList<String>();
int i=1;
//html: *****<img> ******<img >,偶数为img 中含有src的字串
for(String str:imgs){
if(i%2==0){
tmp.add(str.split("src=\"")[1]);
}
i++;
}
for(String str:tmp){
String sp = str.split("\"")[0];
if(sp.indexOf("http")==0){
result.add(sp);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}


private String pipe(InputStream in,String charset) throws IOException {
StringBuffer s = new StringBuffer();
if(charset==null||"".equals(charset)){
charset="utf-8";
}
String rLine = null;
BufferedReader bReader = new BufferedReader(new InputStreamReader(in,charset));

while ( (rLine = bReader.readLine()) != null) {
String tmp_rLine = rLine;
int str_len = tmp_rLine.length();
if (str_len > 0) {
s.append(tmp_rLine);
}
tmp_rLine = null;
}
in.close();
return s.toString();
}
public void download(){
for(int i=currentPage;i>0;i--){
analyse(i);
for(String path:result){
URL url = null;
try {
// 生成图片名
int index = path.lastIndexOf("/");
String sName = path.substring(index+1, path.length()).replace("?","4");
// 存放地址
File img = new File(downLoadPath+sName);
// 如果图片存在或者图片URL中包含以下网址则返回
// 屏蔽几个无法访问的地址加快图片的下载速度
if(img.exists() || path.indexOf("http://www.gravatar")>-1 || path.indexOf("http://www.dxbzyw/")>-1
|| path.indexOf("http://24.media.tumblr")>-1 || path.indexOf("staticflickr")>-1) continue;
BufferedInputStream in = new BufferedInputStream(new URL(path).openStream());
System.out.println(path);
// 生成图片
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(img));
byte[] buf = new byte[2048];
int length = in.read(buf);
while (length != -1) {
out.write(buf, 0, length);
length = in.read(buf);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
result=null;
result = new ArrayList<String>();
try {
// 线程休息10秒
Thread.sleep(1000*10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}


下载图片方法

public class download {

public static void main(String[]a){
jandan f = new jandan ();
f.download();
}
}

更多推荐

下载煎蛋无聊图