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

URL和URLConnection从网络下载资源

2016-08-24 10:31 357 查看
import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

public class HttpUrlConnection {

/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.androidbook.com/akc/filestorage/android/documentfiles/3389/movie.mp4");
URLConnection conn = url.openConnection();
//通过UrlConnection对象获取输入流
InputStream in = conn.getInputStream();
//输出流,指向本地磁盘
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\tupian.mp4")));
int len = -1;
byte[] bys = new byte[1024];
while ((len = in.read(bys)) != -1) {
bos.write(bys,0,len);
}
bos.close();
in.close();
System.out.println("下载完成");
bos.close();
}

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