博客转自:http://www.oschina.net/code/snippet_182121_7941
File file =
new
File(location);
if
(file.exists()) {
long
p =
0
;
long
fileLength;
fileLength = file.length();
// get file content
InputStream ins =
new
FileInputStream(file);
bis =
new
BufferedInputStream(ins);
// tell the client to allow accept-ranges
response.reset();
response.setHeader(
"Accept-Ranges"
,
"bytes"
);
// client requests a file block download start byte
if
(request.getHeader(
"Range"
) !=
null
) {
response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);
p = Long.parseLong(request.getHeader(
"Range"
)
.replaceAll(
"bytes="
,
""
)
.replaceAll(
"-"
,
""
)
);
}
// support multi-threaded download
// respone format:
// Content-Length:[file size] - [client request start bytes from file block]
response.setHeader(
"Content-Length"
,
new
Long(fileLength - p).toString());
if
(p !=
0
) {
// 断点开始
// 响应的格式是:
// Content-Range: bytes [文件块的开始字节]-[文件的总大小 - 1]/[文件的总大小]
String contentRange =
new
StringBuffer(
"bytes "
)
.append(
new
Long(p).toString())
.append(
"-"
)
.append(
new
Long(fileLength -
1
).toString())
.append(
"/"
)
.append(
new
Long(fileLength).toString())
.toString();
response.setHeader(
"Content-Range"
, contentRange);
// pointer move to seek
bis.skip(p);
}
String fileName = file.getName();
response.addHeader(
"Content-Disposition"
,
"attachment;filename="
+ fileName);
while
((size = bis.read(buf)) != -
1
) {
response.getOutputStream().write(buf,
0
,size);
response.getOutputStream().flush();
}
bis.close();
2. [代码]2.客户端下载测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
public
class
TestDownload {
/**
* @param args
*/
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
HttpURLConnection httpURLConnection =
null
;
URL url =
null
;
BufferedInputStream bis =
null
;
byte
[] buf =
new
byte
[
10240
];
int
size =
0
;
String fileName =
"aaa.zip"
;
String filePath =
"C:\\Users\\Desktop"
;
String remoteUrl =
"http://127.0.0.1:8080/down.zip"
;
// 检查本地文件
RandomAccessFile rndFile =
null
;
File file =
new
File(filePath +
"\\"
+ fileName);
long
remoteFileSize = getRemoteFileSzie(remoteUrl);
long
nPos =
0
;
if
(file.exists()) {
long
localFileSzie = file.length();
if
(localFileSzie < remoteFileSize) {
System.out.println(
"文件续传..."
);
nPos = localFileSzie;
}
else
{
System.out.println(
"文件存在,重新下载..."
);
file.delete();
try
{
file.createNewFile();
}
catch
(Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
else
{
// 建立文件
try
{
file.createNewFile();
}
catch
(Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
// 下载文件
try
{
url =
new
URL(remoteUrl);
httpURLConnection = (HttpURLConnection)url.openConnection();
// 设置User-Agent
httpURLConnection.setRequestProperty(
"User-Agent"
,
"Net"
);
// 设置续传开始
httpURLConnection.setRequestProperty(
"Range"
,
"bytes="
+ nPos +
"-"
);
// 获取输入流
bis =
new
BufferedInputStream(httpURLConnection.getInputStream());
rndFile =
new
RandomAccessFile(filePath +
"\\"
+ fileName,
"rw"
);
rndFile.seek(nPos);
int
i =
0
;
while
((size = bis.read(buf)) != -
1
) {
//if (i > 500) break;
rndFile.write(buf,
0
, size);
i++;
}
System.out.println(
"i="
+ i);
httpURLConnection.disconnect();
}
catch
(Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public
static
long
getRemoteFileSzie(String url) {
long
size =
0
;
try
{
HttpURLConnection httpUrl = (HttpURLConnection)(
new
URL(url)).openConnection();
size = httpUrl.getContentLength();
httpUrl.disconnect();
}
catch
(Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return
size;
}
}
|