文件下载的简单代码


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");

}

你可能感兴趣的:(exception,String,File,url,Path,byte)