웹의 주소를 이용해서 파일을 다운받는 작업을 하고있습니다.(myUrl 변수)
현재 소스로 다운로드를 받았을 경우 370mb가 1분30초 정도 걸립니다.
원래 이 정도 속도가 평균적인것인지요..? 코드는 아래와 같이 작성했습니다.
HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
conn.setRequestProperty("Accept-Encoding", "identity");// add
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();