可以下載軟件,可以下載未完成的軟件

功能:可以下載軟件,可以下載未完成的軟件
如果軟件存在,則改名下載,不進行覆蓋,以免勿刪文件
代碼如下:
  1package com.tangshun.www.socket;
  2
  3import java.io.File;
  4import java.io.IOException;
  5import java.io.InputStream;
  6import java.io.RandomAccessFile;
  7import java.net.HttpURLConnection;
  8import java.net.MalformedURLException;
  9import java.net.URL;
 10
 11//斷點續傳
 12public class DownLoad {
 13
 14    public static void down(String URL, long nPos, String savePathAndFile) {
 15        try {
 16            URL url = new URL(URL);
 17            HttpURLConnection httpConnection = (HttpURLConnection) url
 18                    .openConnection();
 19            // 設置User-Agent
 20            httpConnection.setRequestProperty("User-Agent", "NetFox");
 21            // 設置斷點續傳的開始位置
 22            httpConnection.setRequestProperty("RANGE", "bytes=" + nPos);
 23            // 獲得輸入流
 24            InputStream input = httpConnection.getInputStream();
 25            RandomAccessFile oSavedFile = new RandomAccessFile(savePathAndFile,
 26                    "rw");
 27            // 定位文件指針到nPos位置
 28            oSavedFile.seek(nPos);
 29            byte[] b = new byte[1024];
 30            int nRead;
 31            // 從輸入流中讀入字節流,然後寫到文件中
 32            while ((nRead = input.read(b, 0, 1024)) > 0) {
 33                (oSavedFile).write(b, 0, nRead);
 34            }

 35            httpConnection.disconnect();
 36        }
 catch (MalformedURLException e) {
 37            e.printStackTrace();
 38        }
 catch (IOException e) {
 39            e.printStackTrace();
 40        }

 41    }

 42
 43    public static long getRemoteFileSize(String url) {
 44        long size = 0;
 45        try {
 46            HttpURLConnection conn = (HttpURLConnection) (new URL(url))
 47                    .openConnection();
 48            size = conn.getContentLength();
 49            conn.disconnect();
 50        }
 catch (Exception e) {
 51            e.printStackTrace();
 52        }

 53        return size;
 54    }

 55
 56public static void main(String[] args) {
 57        String url = "http://www.videosource.cgogo.com/media/0/16/8678/8678.flv";
 58        String savePath = "F:\\";
 59        String fileName = url.substring(url.lastIndexOf("/"));
 60        String fileNam=fileName;
 61        HttpURLConnection conn = null;
 62        try {
 63            conn = (HttpURLConnection) (new URL(url)).openConnection();
 64        }
 catch (Exception e) {
 65            e.printStackTrace();
 66        }

 67        File file = new File(savePath + fileName);
 68        // 獲得遠程文件大小
 69        long remoteFileSize = getRemoteFileSize(url);
 70        System.out.println("遠程文件大小="+remoteFileSize);
 71        int i = 0;
 72        if (file.exists()) {
 73            // 先看看是否是完整的,完整,換名字,跳出循環,不完整,繼續下載
 74            long localFileSize = file.length();
 75            System.out.println("已有文件大小爲:"+localFileSize);
 76
 77            if (localFileSize < remoteFileSize) {
 78                System.out.println("文件續傳");
 79                down(url, localFileSize, savePath + fileName);
 80            }
else{
 81                System.out.println("文件存在,重新下載");
 82                do{
 83                    i++;
 84                    fileName = fileNam.substring(0, fileNam.indexOf(".")) + "(" + i
 85                            + ")" + fileNam.substring(fileNam.indexOf("."));
 86                    
 87                    file = new File(savePath + fileName);
 88                }
while(file.exists());
 89                try {
 90                    file.createNewFile();
 91                }
 catch (IOException e) {
 92                    e.printStackTrace();
 93                }

 94                down(url, 0, savePath + fileName);
 95            }

 96            // 下面表示文件存在,改名字
 97            
 98        }
 else {
 99            try {
100                file.createNewFile();
101                System.out.println("下載中");
102                down(url, 0, savePath + fileName);
103            }
 catch (IOException e) {
104                e.printStackTrace();
105            }

106        }

107    }
}

108

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