import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SaveImg {
private String webUrl="";
private String imgPath = "f:/img";// 保存文件路径
private String imgContent = "content.txt";// 保存下载的图片名地址对应 文件名:Url列表
public SaveImg(String imgurl) {
webUrl=imgurl;
}
//网页源码到string
public String getHtmlCode(String httpUrl) throws IOException {
if(httpUrl==null || httpUrl.equals(""))
httpUrl=this.webUrl;
StringBuffer content = new StringBuffer();
URL url = new URL(httpUrl);
URLConnection urlconn= url.openConnection();
Reader read = new InputStreamReader(new BufferedInputStream(urlconn
.getInputStream()));
int c;
while((c=read.read())!=-1)
content.append((char)c);
read.close();
return content.toString().replaceAll("\\s*", "");
}
//提取图片url 到list
private List extractImgUrl(String content){
List imgList = new ArrayList();
String imgReg="objURL\":\"http://[\\s\\S]*?(.jpg|.JPG|.png|.PNG|.gif|.GIF)\"";
Matcher imgurlMatcher =Pattern.compile(imgReg).matcher(content);
while (imgurlMatcher.find()) {
imgList.add(imgurlMatcher.group().split("\"")[2]);
}
return imgList;
}
//写图片名,url 到文件。
// imageName:URL
private void writeNameUrl(List imgUrlList) {
try {
File filePath = new File(imgPath);
if (!filePath.exists())
filePath.mkdir();
File f = new File(filePath, "content.txt");
if (!f.exists())
f.createNewFile();
BufferedReader input = new BufferedReader(new FileReader(f));
StringBuffer s1 = new StringBuffer();
String s = new String();
while ((s=input.readLine()) != null) {
s1.append(s+System.getProperty("line.separator"));
}
input.close();
UUID uuid = null;
for(int i=0;i
uuid=UUID.randomUUID();
this.saveImg(uuid.toString(),imgUrlList.get(i).toString());//保存图片
s1.append(uuid+":"+imgUrlList.get(i)+System.getProperty("line.separator"));
}
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1.toString());
output.close();
} catch (IOException e) {
System.err.println(e);
}
}
//保存图片
private boolean saveImg(String imgName,String imgUrl){
URL url;
BufferedInputStream in;
FileOutputStream file;
try {
String fileName = imgName+imgUrl
.substring(imgUrl.lastIndexOf("."));
url = new URL(imgUrl);
in = new BufferedInputStream(url.openStream());
file = new FileOutputStream(new File(imgPath,fileName));
int end;
while ((end = in.read()) != -1) {
file.write(end);
}
file.close();
in.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
SaveImg sim = new SaveImg(
"http://image.baidu.com/i?tn=baiduimage&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1333734805096_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&word=%CE%EF%C0%ED%CA%B5%D1%E9&s=0#pn=15");
try {
sim.writeNameUrl(sim.extractImgUrl(sim.getHtmlCode(null)));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
分享到:
2012-04-07 12:34
浏览 1270
评论