Snowberg

 

文件下载的简单代码


public void downloadFile(String url) {

try {
URL path = null;
try {
path = new URL(url);
} catch (Exception e) {
System.out.println("Error input url");
}
String fileName = url.substring(url.lastIndexOf("/")+1);
FilterInputStream in = (FilterInputStream) path.openStream();
File fileOut = new File("d:\\Temp\\"+fileName);
FileOutputStream out = new FileOutputStream(fileOut);
byte[] bytes = new byte[1024];
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);
}
in.close();
out.close();
} catch (Exception e) {
System.out.println("Error!");
throw new RuntimeException(e);
}
}

@Test
public void test(){
downloadFile("http://imgsrc.baidu.com/baike/pic/item/f765756053ce5274eaf8f80b.jpg");

}

posted on 2011-08-25 10:57  Snowberg  阅读(253)  评论(0编辑  收藏  举报

导航