您的位置:首页 > 编程语言 > C语言/C++

可以下载软件,可以下载未完成的软件

2014-05-12 18:56 274 查看
功能:可以下载软件,可以下载未完成的软件
如果软件存在,则改名下载,不进行覆盖,以免勿删文件
代码如下:

  1

package com.tangshun.www.socket;
  2


  3

import java.io.File;
  4

import java.io.IOException;
  5

import java.io.InputStream;
  6

import java.io.RandomAccessFile;
  7

import java.net.HttpURLConnection;
  8

import java.net.MalformedURLException;
  9

import java.net.URL;
 10


 11

//断点续传
 12

public 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


 56

public 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐