前面整理过基于UDP/TCP协议的网络编程,这里教大家用简单的代码实现下载网络资源的功能。在这里使用到了http协议的相关java类---URL和HttpURLConnection,关于这两个类的深入学习可以查看相关API。
以下载百度上的一张图片为例,代码如下:
package http; //导包 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class CatchPic { /** * 实现从指定网址获取一张图片 * @throws IOException */ public static void main(String[] args) throws IOException { //调用catchPic方法从网络上下载一张图片 catchPic(); } public static void catchPic() throws IOException { //创建URL对象,参数传递一个String类型的URL解析地址 URL url = new URL("http://img04.tooopen.com/images/20131202/sy_49706261893.jpg"); //HttpURLConnection是protected修饰的,无法直接new对象 //url的openConnection方法返回值是URLConnection类的对象,需要强转为HttpURLConnection //所以直接通过url调用openConnection方法来实现创建HttpURLConnection的对象 HttpURLConnection huc =(HttpURLConnection) url.openConnection(); //设置请求方式 huc.setRequestMethod("GET"); int code=0; // 从 HTTP 响应消息获取状态码 //200表示ok,500表示服务器错误,404表示找不到 code =huc.getResponseCode(); //如果状态码为200,表示连接ok,可以继续操作 if(code==200) { //获取输入流 InputStream ips = huc.getInputStream(); //采用高效输入流获取网络上的资源 BufferedInputStream bis=new BufferedInputStream(ips); BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream("/home/sker/桌面/meitu.jpg")); byte[] b=new byte[1024]; int len=0; //将数据写入文件 while((len=bis.read(b))!=-1) { bos.write(b,0,len); //刷新资源------在前面IO部分提到过,write方法使用完需要刷新 bos.flush(); } //关闭流,释放资源 bos.close(); bis.close(); ips.close(); } } }如下是网络上的资源以及下载到本地的资源图