package com.aotu.jianantong.http.download.downTask;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import com.aotu.jianantong.utils.Logs;
/**
* 类说明 :调用.execute();
* @version 创建时间:2016年8月1日 下午4:46:55
*
*/
public class DownloadTask extends AsyncTask<String, Integer, Void> {
private static final String TAG = "test";
private String downloadUrl;
private String saveLocalPath;
private Context ctx;
DownLoadProgressListener mDownLoadProgressListener;
private boolean isAllNormal = true;
public DownloadTask(Context context,
DownLoadProgressListener mDownLoadProgressListener,
String downloadUrl, String saveLocalPath) {
this.ctx = context;
this.mDownLoadProgressListener = mDownLoadProgressListener;
this.downloadUrl = downloadUrl.trim();
this.saveLocalPath = saveLocalPath.trim();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mDownLoadProgressListener.startDownLoad();
}
@Override
protected Void doInBackground(String... params) {
try {
URL url = new URL(downloadUrl);
Logs.d(TAG, "downloadUrl =" + downloadUrl);
HttpURLConnection httpurlCon = (HttpURLConnection) url
.openConnection();
Log.d("test",
"DownloadTask ResponseCode " + httpurlCon.getResponseCode());
if (httpurlCon.getResponseCode() == 404) {
isAllNormal = false;
return null;
}
File f = null;
try {
f = new File(saveLocalPath.substring(0,
saveLocalPath.lastIndexOf("/") + 1));
Logs.d(TAG,
"DownloadTask 路径"
+ saveLocalPath.substring(0,
saveLocalPath.lastIndexOf("/") + 1));
if (!f.exists()) {
f.mkdirs();
} else {
Logs.d(TAG, "DownloadTask 无写入权限(创建文件夹)");
return null;
}
File ff = new File(saveLocalPath);
if (!ff.exists()) {
ff.createNewFile();
} else {
Logs.d(TAG, "DownloadTask 无写入权限(创建文件)");
return null;
}
} catch (Exception e) {
isAllNormal = false;
}
FileOutputStream fos = new FileOutputStream(saveLocalPath);
InputStream instream = httpurlCon.getInputStream();
int file_legth = httpurlCon.getContentLength();
int len = 0;
byte[] buffer = new byte[1024];
int total_legth = 0;
while ((len = instream.read(buffer)) != -1) {
fos.write(buffer, 0, len);
total_legth += len;
int value = (int) ((total_legth / (float) file_legth) * 100);
publishProgress(value);
}
fos.close();
instream.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
mDownLoadProgressListener.downLoadProgress(values[0]);
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (isAllNormal) {
mDownLoadProgressListener.downLoadSuccess(downloadUrl,
saveLocalPath);
} else {
mDownLoadProgressListener
.downLoadFailed(downloadUrl, saveLocalPath);
}
mDownLoadProgressListener.endDownLoad(downloadUrl, saveLocalPath);
}
public interface DownLoadProgressListener {
public void startDownLoad();
public void downLoadProgress(int downLoadProgress);
/**
* 方法说明:
*
* @version 创建时间:2016年7月21日 下午5:29:22
*
* @param downloadUrl
* @param saveLocalPath
*/
public void endDownLoad(String downloadUrl, String saveLocalPath);
/**
* 方法说明:暂未使用
*
* @version 创建时间:2016年7月21日 下午4:00:54
*
*/
public void downLoadFailed(String downloadUrl, String saveLocalPath);
public void downLoadSuccess(String downloadUrl, String saveLocalPath);
}
}