java 抓取网页图片并保存
2015-03-08 14:45
537 查看
public class CatchWebPicture {
@Test
public void test_getWebPicture() {
try {
File path = null;
URL url = null;
for(int i=0;i<72;i++){
url = new URL("http://www.skyatnightmagazine.com/360/wifi%20SCOPE/img/threesixty_"+array[i]+".jpg");
URLConnection urlCon = url.openConnection();
String str = url.getFile();
StringTokenizer st = new StringTokenizer(str, "/");
String sub = null;
while (st.hasMoreTokens()) {
sub = st.nextToken();
}
path = new File("C:/Users/Administrator/Desktop/threesixty/" + sub);
InputStream is = urlCon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
FileOutputStream fos = new FileOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int read;
while ((read = bis.read()) != -1) {
bos.write(read);
}
bos.close();// 不关闭,输出流不刷新,有可能得到无效图片
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Test
public void test_getWebPicture() {
try {
File path = null;
URL url = null;
for(int i=0;i<72;i++){
url = new URL("http://www.skyatnightmagazine.com/360/wifi%20SCOPE/img/threesixty_"+array[i]+".jpg");
URLConnection urlCon = url.openConnection();
String str = url.getFile();
StringTokenizer st = new StringTokenizer(str, "/");
String sub = null;
while (st.hasMoreTokens()) {
sub = st.nextToken();
}
path = new File("C:/Users/Administrator/Desktop/threesixty/" + sub);
InputStream is = urlCon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
FileOutputStream fos = new FileOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int read;
while ((read = bis.read()) != -1) {
bos.write(read);
}
bos.close();// 不关闭,输出流不刷新,有可能得到无效图片
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
相关文章推荐
- Java Jsoup抓取符合指定大小的网页的图片,并保存在本地磁盘
- 黄聪:python访问抓取网页常用命令(保存图片到本地、模拟POST、GET、中文编码问题)
- JAVA抓取网页的图片,JAVA利用正则表达式抓取网站图片
- python抓取网页中图片并保存到本地
- 抓取整个网页保存为图片的实用工具 Web2Pic Pro
- JAVA抓取网页的图片,JAVA利用正则…
- 一个简单的java读取网页图片并保存图片的程序
- 黄聪:python访问抓取网页常用命令(保存图片到本地、模拟POST、GET、中文编码问题)
- 分享PHP源码批量抓取远程网页图片并保存到本地的实现方法
- python 网页抓取并保存图片
- python抓取网页中图片并保存到本地
- JAVA实现自动打开URL对应的网页并保存为图片-不借助第三方API
- 利用java抓取网页某个图片
- java如何保存网页上的图片
- 分享PHP源码批量抓取远程网页图片并保存到本地的实现方法
- 用java抓取网页图片
- 关于java抓取网页数据与图片
- java 获取网页(络)图片并保存
- java爬虫实战(1):抓取信息门户网站中的图片及其他文件并保存至本地
- python抓取网页中图片并保存到本地