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();
}
}
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();
}
}
相关文章推荐
- 通过URL连接下载网络资源(基本)
- URL网络资源下载
- 疯狂安卓实训 第13.2.1讲 使用URL和URLConnection访问网络资源
- 简单使用URLConnection、HttpURLConnection和HttpClient访问网络资源
- 通过URL获取并下载网络资源
- Android中实现下载URL地址的网络资源的实例分享
- 简单使用URLConnection、HttpURLConnection和HttpClient访问网络资源
- Android 网络:使用URL访问网络资源,ImageView,下载到本地
- 简单使用URLConnection、HttpURLConnection和HttpClient访问网络资源
- 通过URL来获取网络上的资源下载
- 通过URL类和网络建立连接,从网络上下载资源
- HttpUrlConnection实现多线程下载网络资源
- 简单使用URLConnection、HttpURLConnection和HttpClient访问网络资源
- 学习url读取网络资源,开启线程来下载图片
- android之通过URL来获取网络资源并下载资源简单实例
- 简单使用URLConnection、HttpURLConnection和HttpClient访问网络资源
- 根据url从网络上下载资源
- 通过URL获取网络资源
- [Android]网络资源下载时断点续传的实现
- Android手机开发:网络连接-打开Url下载信息