可以下载软件,可以下载未完成的软件
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
如果软件存在,则改名下载,不进行覆盖,以免勿删文件
代码如下:
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
相关文章推荐
- 我把一些常用的好软件放在我的QQ网络硬盘里面,有需要的朋友可以去下载
- 可以媲美TortoiseSVN的LINUX版SVN客户端软件RabbitVCS(下载地址)
- 7-zip命令行解压缩工具使用说明(可以到我的下载区去下载此软件)
- 用delphi 实现文件下载,可以用于软件的升级
- inno setup 软件安装包 如何可以自动检测、下载并安装.NET环境
- 可以上传、下载文件的SSH客户端软件--SecureCRT绿色版
- 分享一个无需下载其他软件并且可以做到手机计算机三种互传的网站
- 网络采集软件核心技术剖析系列(3)---如何使用C#语言下载博文中的全部图片到本地并可以离线浏览
- PS3.Proxy 为PS3做的代理服务器软件,可以替换下载的文件!
- windows 平台下生成 hash 值的几款软件,支持的算法有 md5 、sha1 等,可以校验下载文件的完整性
- 请问下载那种软件可以使模糊的照片变清晰?谢谢~_百度知道
- 免费下载Mac读写软件的地方 大家可以记一下
- 精品软件 推荐 酷我音乐 一个可以下载320k 音质的音乐播放软件
- 开源DIGG软件PBDigg2.0的修改(七):必须发表评论才可以下载附件。
- 我的新软件ORACLE DBEXPORT2.5可以在本BLOG中下载了
- redhat5.8+pam+mysql+vsftp(提供软件包可以到 百度云盘下载 账号 pankuo1@sina.cn 密码pk492940446)
- 自己做的几个小软件(数学工具和游戏),用C/C#制作,用到许多相关的C#技术细节,可以免费提供下载,感兴趣的,来看一下
- 下载什么录屏软件可以录制游戏视频?
- 软件升级公告:最新版本V1.2.05.115 更新日期2011-9-18 有需要升级的朋友可以到服务器上下载