java获取页面的图片
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class 下载指定页面上的图片{
public static void main(String[] args) {
try{
Map<String,String> map=getPicList("http://tieba.baidu.com/tb/picture/index.html");
Set<String> set=map.keySet();
System.out.println(set.size()+":"+map.size());
}catch(Exception e){
e.printStackTrace();
}
System.out.println("成功");
}
public static void save(String u,String fileName)throws Exception{
URL url=new URL(u);
InputStream input=url.openStream();
FileOutputStream out=new FileOutputStream(new File("C:\\Users\\wy\\Desktop\\uploaded\\"+fileName));
int count=0;
byte b[]=new byte[1024];
while((count=input.read(b))!=-1){
out.write(b,0,count);
}
out.flush();
out.close();
}
public static Map<String,String> getPicList(String u)throws Exception{
URL url=new URL(u);
URLConnection con=url.openConnection();
InputStream input=con.getInputStream();
Map map=new HashMap();
BufferedReader reader=new BufferedReader(new InputStreamReader(input));
String str="";
while((str=reader.readLine())!=null){
java获取页面的图片 - wangyue.123.com - wangyue.123.com的博客
if(str.contains("http")&&str.contains("jpg")){
try{
String uri=str.substring(str.indexOf("http"),str.indexOf("jpg")+3);
String fileName=uri.substring(uri.lastIndexOf("/")+1);
System.out.println(fileName+"==="+uri);
map.put(fileName,uri);
}catch(StringIndexOutOfBoundsException e){
e.printStackTrace();
System.out.println("出现问题的行"+str);
}
}
}
return map;
}
}