URL,下载网页图片。
2016-03-05 22:56
246 查看
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.UUID;
//下载网站图片并保存在D盘
public class TestURL {
public static void main(String[] args) throws IOException {
URL url = new URL("http://b.zol-img.com.cn/desk/bizhi/image/3/1680x1050/1376276329221.jpg"); //下载图片的网页地址
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
String contentType = conn.getContentType();//image/jpeg
String ext = "";
if("image/jpeg".equals(contentType)){
ext = ".jpg";
}else if("image/png".equals(contentType)){
ext = ".png";
}else if("image/bmp".equals(contentType)){
ext = ".bmp";
}else if("image/gif".equals(contentType)){
ext = ".gif";
}
//输入流
OutputStream out = new FileOutputStream("D:\\"+UUID.randomUUID()+ext);//UUID是唯一的标识字符串名
byte[] buf = new byte[1024*4];
int len = -1;
while((len=in.read(buf))!=-1){
out.write(buf,0,len);
}
out.flush();
System.out.println("下载完成");
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.UUID;
//下载网站图片并保存在D盘
public class TestURL {
public static void main(String[] args) throws IOException {
URL url = new URL("http://b.zol-img.com.cn/desk/bizhi/image/3/1680x1050/1376276329221.jpg"); //下载图片的网页地址
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
String contentType = conn.getContentType();//image/jpeg
String ext = "";
if("image/jpeg".equals(contentType)){
ext = ".jpg";
}else if("image/png".equals(contentType)){
ext = ".png";
}else if("image/bmp".equals(contentType)){
ext = ".bmp";
}else if("image/gif".equals(contentType)){
ext = ".gif";
}
//输入流
OutputStream out = new FileOutputStream("D:\\"+UUID.randomUUID()+ext);//UUID是唯一的标识字符串名
byte[] buf = new byte[1024*4];
int len = -1;
while((len=in.read(buf))!=-1){
out.write(buf,0,len);
}
out.flush();
System.out.println("下载完成");
}
}
相关文章推荐
- BZOJ2660/Beijing wc2012 最多的方案
- [Unity优化]unity中的优化方法
- win8、win8.1系统查看连接过的WiFi的密码
- [读书笔记]恰如其分的软件架构-概述
- android stuido 报 If you are behind an HTTP proxy, please configure the proxy set
- 冒泡排序(python版)
- 今日BBC
- wordpress爆破数据
- <考古笔记>hge游戏引擎(一)
- jquery 划词百度(谷歌、必应等)搜索
- 【BZOJ 3545】【ONTAK 2010】Peaks & 【BZOJ 3551】【ONTAK 2010】Peaks加强版 Kruskal重构树
- JVM是什么?
- 线段树代码整理
- Yahoo性能优化35条军规
- 关于毕业设计——面向运动的无线体域网系统设计的构想
- HBase笔记(一)
- 西南BANG 之 android学习笔记1——5个android UI 布局
- 【GDOI 模拟3.5】总结
- UVA 12503(模拟)
- POJ 1004