您的位置:首页 > 编程语言 > Java开发

java下载网页链接上的图片

2014-10-20 13:35 495 查看
public static void main(String[] args) throws Exception {
download("http://www.kadang.com/static/mobile/weixin/hd/t-shirt2.jpg", "53bi.gif","D:\\image\\");
}

public static void download(String urlString, String filename,String savePath) throws Exception {
URL url=new URL(urlString);
URLConnection connection=url.openConnection();//打开链接
connection.setConnectTimeout(5*1000);
InputStream input=connection.getInputStream();

byte[] bs=new byte[1024];
int length;
File file=new File(savePath);
if (!file.exists()) {
file.mkdirs();
}

OutputStream out=new FileOutputStream(file.getPath()+"\\"+filename);
System.out.println(file.getPath());
while ((length=input.read(bs))!=-1) {
out.write(bs,0,length);
}
out.close();
input.close();
System.out.println("下载结束");
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: