URL:統一資源定位器,其實就是資源的路徑。
URL的格式:protocal://host:port/resourcename.
比如:http://www.baidu.com/xxx.txt;
URLConnection:代表與URL的連接,URL.openConnection()方法就是返回的一個URLConnection對象,代表與那個URL資源的連接
它是所有網絡連接的父類,有HttpURLConnection, JarURLConnection等子類。
下面是《java瘋狂講義》的一個實例,實現了一個下載的工具類。
import java.net.*;
import java.io.*;
import java.io.RandomAccessFile;
public class DownUtil {
private String path;//保存路徑
private String targetfile;//文件保存位置
private int threadnum;//用於下載的線程數
private downthread[] threads;//定義線程對象數組
private int filesize;//定義文件大小
public void Downutil(String path,String targetfile,int threadnum)//構造函數
{
this.path=path;
this.targetfile=targetfile;
this.threadnum=threadnum;
}
public void download() throws Exception
{
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();//URLConnetcion類是所有url連接的父類。
conn.setConnectTimeout(5*1000);
conn.setRequestMethod("GET");//註釋一
conn.setRequestProperty("Accept", "image/gif,image/jpeg,image/pjpeg,image/jpeg,"
+"application/x-shockwave-flash,application/xaml+xml,"
+"application/vnd.ms-powerpoint,application/msword,*/*");
conn.setRequestProperty("charset", "UTF-8");
conn.setRequestProperty("Accept-Lanuage", "zh-CN");
conn.setRequestProperty("Connection", "Keep-Alive");
filesize=conn.getContentLength();
conn.disconnect();
int currentpartsize=filesize/threadnum+1;
RandomAccessFile file =new RandomAccessFile(targetfile,"rw");//註釋二
file.setLength(filesize);
file.close();
for(int i=0;i<threadnum;i++)
{
int startpos=i*currentpartsize;//當前循環的線程的下載起始位置
RandomAccessFile currentpart=new RandomAccessFile(targetfile,"file");
currentpart.seek(startpos);//找到起始下載點
threads[i]= new downthread(startpos,currentpartsize,currentpart);
threads[i].start();//當前線程開始
}
}
public double getcompleterate()//返回現在下載了多少了的百分比
{
int sumsize=0;
for (int i=0;i<threadnum;i++)
{
sumsize+=threads[i].length;
}
return sumsize*1.0/filesize;
}
private class downthread extends Thread
{
private int startpos;
private int currentpartsize;
private RandomAccessFile currentpart;
public int length;
public downthread(int startpos,int currentpartsize,RandomAccessFile currentpart)
{
this.startpos=startpos;
this.currentpartsize=currentpartsize;
this.currentpart=currentpart;
}
public void run()
{
try
{
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5*1000);//爲連接設置http信息頭
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "image/gif,image/jpeg,image/pjpeg,image/jpeg,"
+"application/x-shockwave-flash,application/xaml+xml,"
+"application/vnd.ms-powerpoint,application/msword,*/*");//註釋三
conn.setRequestProperty("charset", "UTF-8");
conn.setRequestProperty("Accept-Lanuage", "zh-CN");
InputStream instream=conn.getInputStream();
instream.skip(this.startpos);
byte[] buffer =new byte[1024];
int hasread=0;
while(length<currentpartsize&&(hasread=instream.read(buffer))!=-1)
{
currentpart.write(buffer,0,hasread);
length+=hasread;
}
currentpart.close();
instream.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
註釋一:Http定義了與服務器交互的不同方法,最基本的方法有4種,分別是GET,POST,PUT,DELETE。URL全稱是資源描述符,我們可以這樣認爲:一個URL地址,它用於描述一個網絡上的資源,而HTTP中的GET,POST,PUT,DELETE就對應着對這個資源的查,改,增,刪4個操作,GET一般用於獲取/查詢資源信息,而POST一般用於更新資源信息。
註釋二:RandomAccessFile是一個獨立的訪問數據文件的類,不屬於輸入輸出流,seek()方法能在文件裏移動。
註釋三:setrequestproperty()是定義的http信息頭。
Connection 表明客戶端是否可以處理HTTP持久連接。持久連接允許客戶端或瀏覽器在一個請求中獲取多個文件。Keep-Alive 表示啓用持久連接
Accept 指定瀏覽器或其他客戶端可以處理的MIME類型。它的值通常爲 image/png 或 image/jpeg
參考:菜鳥教程