private
class
test
extends
AsyncTask<String, String, String> {
private
ProgressDialog mDlg;
@Override
protected
String doInBackground(String... params) {
String suc =
"F"
;
int
count =
0
;
long
now = System.currentTimeMillis();
String localPath = myPath + now +
".mp3"
;
InputStream isFile =
null
;
FileOutputStream fosFile =
null
;
try
{
URL myUrl =
new
URL(pinUrl3);
URI uri =
new
URI(myUrl.getProtocol(), myUrl.getUserInfo(), myUrl.getHost(), myUrl.getPort(),
myUrl.getPath(), myUrl.getQuery(), myUrl.getRef());
myUrl = uri.toURL();
HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
conn.setRequestProperty(
"Accept-Encoding"
,
"identity"
);
conn.setConnectTimeout(
2000
);
int
fileSize = conn.getContentLength();
double
kilobytes = (fileSize /
1024
);
double
megabytes = (kilobytes /
1024
);
Log.e(
"다운로드파일사이즈:"
, megabytes +
"MB"
);
if
(fileSize <
0
) {
conn.disconnect();
suc =
"F"
;
}
else
{
byte
[] data =
new
byte
[fileSize];
InputStream 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
) total *
100
/ fileSize;
publishProgress(
""
+ progress_temp);
if
(progress_temp %
10
==
0
&& progress != progress_temp)
{
progress = progress_temp;
}
if
(read <=
0
)
{
break
;
}
fos.write(data,
0
, read);
}
is.close();
fos.close();
conn.disconnect();
suc =
"T"
;
}
}
}
catch
(ConnectTimeoutException e) {
e.printStackTrace();
}
catch
(Exception e) {
e.printStackTrace();
}
return
suc;
}
private
void
publishProgress(String string)
{
Log.e(
"publishProgress"
,Integer.parseInt(string)+
""
);
mDlg.setProgress(Integer.parseInt(string));
}