HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
conn.setRequestProperty(
"Accept-Encoding"
,
"identity"
);
conn.setConnectTimeout(
2000
);
InputStream is =
null
;
int
statusCode = conn.getResponseCode();
if
(statusCode >=
200
&& statusCode <
400
)
{
is = conn.getInputStream();
}
else
{
is = conn.getErrorStream();
}
int
fileSize = conn.getContentLength();
if
(fileSize <
0
)
{
conn.disconnect();
downResult =
1
;
}
else
{
byte
[] data =
new
byte
[
1024
];
is = conn.getInputStream();
File file =
new
File(localPath);
if
(!file.exists())
{
FileOutputStream fos =
new
FileOutputStream(file);
int
read;
long
total =
0
;
int
progress =
0
;
for
(;;)
{
read = is.read(data);
total += read;
int
progress_temp = (
int
) ((
double
) total / fileSize *
100
);
publishProgress(
""
+ progress_temp);
if
(progress_temp %
10
==
0
&& progress != progress_temp)
{
progress = progress_temp;
}
if
(read <=
0
) {
break
;
}
fos.write(data,
0
, read);
if
(isCancelled())
{
return
4
;
}
}
is.close();
fos.close();
conn.disconnect();