Java網絡編程_基本網絡支持(二)

URL、URLConnection和URLPermission

URL對象代表統一資源定位器,它是對指向互聯網“資源”的指針。資源可以是簡單的文件或目錄,也可以是對更爲複雜對象的引用,例如對數據庫或搜索引擎的查詢。在通常情況下,URL可以由協議名、主機、端口和資源組成。

URL類提供了多個構造器用於創建URL對象,一旦獲得了URL對象之後,就可以調用如下方法來訪問URL對應的資源。

方法
String getFile() 獲取該URL的資源名
String getHost() 獲取該URL的主機名
String getPath() 獲取該URL的路徑部分
int getPort() 獲取該URL的端口號
String getProtocol() 獲取該URL的協議名稱
String getQuery() 獲取該URL的查詢字符串部分
URLConnection openConnection() 返回一個URLConnection對象,它代表了與URL所引用的遠程對象的連接
InputStream openStream() 打開於此URL的連接,並返回一個用於讀取該URL資源的InputStream

測試URL類方法的代碼:

public class URLTest {

    public static void main(String[] args) throws Exception{

        URL url = new URL("http://img2.ph.126.net/Mpr7kRqQ51wTt7KHYmJI6A==/6619508599956323849.jpg");
        System.out.println("URL資源名:" + url.getFile());
        System.out.println("URl主機名:" + url.getHost());
        System.out.println("URL路徑:" + url.getPath());
        System.out.println("URL端口號:" + url.getPort());
        System.out.println("URL協議:" + url.getProtocol());
        System.out.println("URL查詢字符串部分:" + url.getQuery());
    }
}

測試結果:

URL資源名:/Mpr7kRqQ51wTt7KHYmJI6A==/6619508599956323849.jpg
URl主機名:img2.ph.126.net
URL路徑:/Mpr7kRqQ51wTt7KHYmJI6A==/6619508599956323849.jpg
URL端口號:-1
URL協議:http
URL查詢字符串部分:null

使用openStream()方法可以讀取該URL資源的InputStream,通過該方法可以非常方便地讀取遠程資源——甚至實現多線程下載。

實現步驟:
1.創建URL對象。
2.獲取指定URL對象所指向資源的大小(通過getContentLength()方法獲得)。
3.在本地磁盤上創建一個與網絡資源具有相同大小的空文件。
4.計算每個線程應該下載網絡資源的哪個部分(從哪個字節開始,到哪個字節結束)。
5.依次創建、啓動多個線程來下載網絡資源的指定部分。

下載工具類代碼的實現:

public class DownUtil {

    //定義下載資源路徑
    private String path;
    //指定所下載的文件保存位置
    private String targetFile;
    //定義需要使用多少個線程下載資源
    private int threadNum;
    //定義下載的線程對象
    private DownThread[] threads;
    //定義下載的文件的總大小
    private int fileSize;

    public DownUtil(String path, String targetFile, int threadNum){

        this.path = path;
        this.threadNum = threadNum;
        //初始化threads數組
        threads = new DownThread[threadNum];
        this.targetFile = targetFile;       
    }

    public void download() throws Exception{

        URL url = new URL(path);
        //URLConnection表示應用程序和URL之間的通信連接
        //HttpURLConnection表示與URL之間的http連接
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        //連接超時設置
        conn.setConnectTimeout(5 * 1000);
        //請求獲取Request-URI所標識的資源
        conn.setRequestMethod("GET");
        //客戶端的配置,需求
        //setRequestProperty()可以不設置
        conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, "
                + "application/x-shockwave-flash, application/xaml+xml,"
                + "application/vnd.ms-xpsdocument, application/x-ms-xbap,"
                + "application/x-ms-application, application/vnd.ms-excel,"
                + "application/vnd.ms-powerpoint, application/msword, */*");
        //支持語言
        conn.setRequestProperty("Accept-Language", "zh-CN");
        //返回的字符
        conn.setRequestProperty("Charset", "UTF-8");
        //連接方式
        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進行下載
            RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");
            //定位該線程的下載位置
            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;
        //定義該線程已下載的字節數
        private 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);
                //請求獲取Request-URI所標識的資源
                conn.setRequestMethod("GET");
                //客戶端的配置,需求
                conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, "
                        + "application/x-shockwave-flash, application/xaml+xml,"
                        + "application/vnd.ms-xpsdocument, application/x-ms-xbap,"
                        + "application/x-ms-application, application/vnd.ms-excel,"
                        + "application/vnd.ms-powerpoint, application/msword, */*");
                //支持語言
                conn.setRequestProperty("Accept-Language", "zh-CN");
                //返回的字符
                conn.setRequestProperty("Charset", "UTF-8");
                InputStream inStream = conn.getInputStream();
                //跳過startPos個字節,表明該線程只下載自己負責的那部分文件
                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();
            }
        }       
    }
}

主程序代碼:

public class MultiThreadDown {

    public static void main(String[] args) throws Exception{

        //初始化DownUtil對象
        final DownUtil downUtil = new DownUtil("http://s1.dwstatic.com/group1/"
                + "M00/65/09/5e4f2b1472094ccaf7b7f2fda38b01d9.gif", 
                "1.png", 4);
        downUtil.download();
        new Thread(() -> {
            while(downUtil.getCompleteRate() < 1){
                //每隔0.1秒查詢一次任務得完成速度
                //GUI程序中可根據該進度來繪製進度條
                System.out.println("已完成:" + downUtil.getCompleteRate());
                try{
                    Thread.sleep(1000);
                }catch (Exception e){}
            }
        }).start();;
    }
}

運行程序可以下載到一張圖片。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章