斷點續傳 Java版(原)

 功能:可以下載軟件,可以下載未完成的軟件
如果軟件存在,則改名下載,不進行覆蓋,以免勿刪文件
代碼如下:
package com.test.day7.down;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author BigBird
 * @date 2011-9-10 上午08:34:57
 * @action
 */
public class DownLoad {

	public static void down(String URL, long nPos, String savePathAndFile) {

		try {
			URL url = new URL(URL);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			// 設置User-Agent
			connection.setRequestProperty("User-Agent", "NetFox");
			// 設置斷點續傳的開始位置
			connection.setRequestProperty("RANGE", "bytes=" + nPos);

			// 獲得輸入流
			InputStream input = connection.getInputStream();

			RandomAccessFile accessFile = new RandomAccessFile(savePathAndFile,
					"rw");
			// 定位文件指針到nPos位置
			accessFile.seek(nPos);

			byte[] b = new byte[1024];
			int nRead;

			// 從輸入流中讀入字節流,然後寫到文件中
			while ((nRead = input.read(b, 0, 1024)) > 0) {
				(accessFile).write(b, 0, nRead);
			}

			connection.disconnect();
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static long getRemoteFileSize(String url) {
		long size = 0;

		try {
			HttpURLConnection httpConnection = (HttpURLConnection) (new URL(url)
					.openConnection());
			size = httpConnection.getContentLength();
			httpConnection.disconnect();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return size;
	}

	public static void main(String[] args) {
		String url = "http://www.videosource.cgogo.com/media/0/16/8678/8678.flv";
		String savePath = "E://Test File//";
		String fileName = url.substring(url.lastIndexOf("/"));
		String fileName2 = fileName;

		HttpURLConnection conn = null;

		try {
			conn = (HttpURLConnection) (new URL(url).openConnection());
		} catch (Exception e) {
			e.printStackTrace();
		}

		File file = new File(savePath + fileName2);

		// 獲得遠程文件大小
		long fileSize = getRemoteFileSize(url);
		System.out.println("遠程文件大小=" + fileSize);

		int i = 0;
		if (file.exists()) {
			// 先看看是否是完整的,完整,換名字,跳出循環,不完整,繼續下載
			long localFileSize = file.length();
			System.out.println("已有文件大小爲:" + localFileSize);

			if (localFileSize < fileSize) {
				System.out.println("文件續傳");
				down(url, localFileSize, savePath + fileName);
			} else {
				System.out.println("文件存在,重新下載");
				do {
					i++;
					fileName = fileName2.substring(0, fileName2.indexOf("."))
							+ "(" + i + ")"
							+ fileName2.substring(fileName2.indexOf("."));
					file = new File(savePath + fileName);
				} while (file.exists());

				try {
					file.createNewFile();
					down(url, 0, savePath + fileName);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} else { // 下面表示文件存在,改名字

			try {
				file.createNewFile();
				down(url, 0, savePath + fileName2);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

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