Java 从网络上下载文件
生活随笔
收集整理的這篇文章主要介紹了
Java 从网络上下载文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/**
?*?下載文件到本地?
?*/
public?static?void?downloadPicture(String?imageUrl,?String?filename){??
????URL?url;
try?{
url?=?new?URL(imageUrl);
??//打開網(wǎng)絡(luò)輸入流
????DataInputStream?dis?=?new?DataInputStream(url.openStream());
????//建立一個新的文件
????FileOutputStream?fos?=?new?FileOutputStream(new?File(filename));
????byte[]?buffer?=?new?byte[1024];
????int?length;
????//開始填充數(shù)據(jù)
????while((length?=?dis.read(buffer))>0){
???? fos.write(buffer,0,length);
????}
????dis.close();
????fos.close();
}?catch?(Exception?e)?{
e.printStackTrace();
}
??
}
?*?下載文件到本地?
?*/
public?static?void?downloadPicture(String?imageUrl,?String?filename){??
????URL?url;
try?{
url?=?new?URL(imageUrl);
??//打開網(wǎng)絡(luò)輸入流
????DataInputStream?dis?=?new?DataInputStream(url.openStream());
????//建立一個新的文件
????FileOutputStream?fos?=?new?FileOutputStream(new?File(filename));
????byte[]?buffer?=?new?byte[1024];
????int?length;
????//開始填充數(shù)據(jù)
????while((length?=?dis.read(buffer))>0){
???? fos.write(buffer,0,length);
????}
????dis.close();
????fos.close();
}?catch?(Exception?e)?{
e.printStackTrace();
}
??
}
總結(jié)
以上是生活随笔為你收集整理的Java 从网络上下载文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LVS在淘宝环境中的应用
- 下一篇: Java中的代码块标记