您的位置:首页 > 理论基础 > 计算机网络

网络图片下载并保存本地

2014-12-23 18:16 459 查看
<pre name="code" class="java">@Test
public  void getFile(){
String fileUrl="http://static.zuchecdn.com/images/order/hg.jpg";
Integer fileId=1;
String path="d:/csms/"+fileId+".jpg";
System.out.print(path);
try{

URL url=new URL(fileUrl);

HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.connect();
InputStream is=connection.getInputStream();
OutputStream os=new FileOutputStream(path);
BufferedOutputStream buffer=new BufferedOutputStream(os);
byte[] b=new byte[1024];
int len=0;
while((len=is.read(b))>0){
buffer.write(b,0,len);
}
buffer.flush();
os.close();
is.close();
}catch (Exception e) {
e.printStackTrace();
}
}



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