/**
* Object 下载地址
* Integer 进度
* File 下载完成后保存到File
*/
public class DownLoadFileTask extends AsyncTask<Object, Integer, File> {
private static final String TAG = "DownLoadFileTask";
private ProgressDialog mProgressDialog;
private Context context;
private String fileName;
private HttpURLConnection http;
private int errorCode;
// private String filePath;
public DownLoadFileTask(Context context){
this.context = context;
}
public DownLoadFileTask(Context context,String fileName){
this.context = context;
this.fileName = fileName;
}
protected File doInBackground(Object... views) {
String FileUrlStr = (String) views[0];
int count = 0;
int fileSize = 0;
File outFile = null;
try {
URL fileUrl = new URL(FileUrlStr);
http = (HttpURLConnection) fileUrl.openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Accept","image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
http.setRequestProperty("Accept-Language", "zh-CN");
http.setRequestProperty("Referer", fileUrl.toString());
http.setRequestProperty("Charset", "UTF-8");
http.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
http.setRequestProperty("Connection", "Keep-Alive");
Log.e(TAG, http.getResponseCode()+"========http.getResponseCode");
if(http.getResponseCode() == 200){
fileSize = http.getContentLength();
File sdCardDir = Utils.getSaveDir();
// Log.e(TAG, fileSize+"文件大小");
// Log.e(TAG, sdCardDir.getAbsolutePath());
if(!sdCardDir.exists()){
sdCardDir.mkdirs();
// Log.e(TAG, "创建附件保存文件夹成功");
}
if (fileSize <= 0) {
Log.e(TAG, "服务器地址错误!");
Toast.makeText(context, "附件大小为0,停止下载", Toast.LENGTH_SHORT).show();
return null;
}
mProgressDialog.setMax(fileSize);
InputStream inStream = http.getInputStream();
String contentEncodingStr = http.getContentEncoding();
// Log.i("DownLoadFileTask", "contentEncodingStr="+contentEncodingStr);
outFile = new File(sdCardDir , fileName);
ServiceManagerApplication.filePath = outFile.getAbsolutePath();
if("gzip".equalsIgnoreCase(contentEncodingStr)) {
GZIPInputStream inputGzipInputStream = new GZIPInputStream(inStream);
byte[] buffer = new byte[1024];
int len = -1;
FileOutputStream fos=new FileOutputStream(outFile);
while( (len = inputGzipInputStream.read(buffer)) != -1){
fos.write(buffer, 0, len);
count += len;
publishProgress(count);
}
fos.close();
inStream.close();
inputGzipInputStream.close();
} else {
byte[] buffer = new byte[1024];
int len = -1;
FileOutputStream fos=new FileOutputStream(outFile);
while( (len = inStream.read(buffer)) != -1){
fos.write(buffer, 0, len);
count += len;
publishProgress(count);
}
fos.close();
inStream.close();
}
}else{
Log.e(TAG, "服务器下载配置错误");
errorCode = http.getResponseCode();
// Toast.makeText(context, "服务器错误码:"+http.getResponseCode(), Toast.LENGTH_SHORT).show();
}
}catch (Exception e) {
e.printStackTrace();
return null;
}
return outFile;
}
@Override
protected void onProgressUpdate(Integer... values) {
mProgressDialog.setProgress(values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onCancelled() {
if(mProgressDialog != null){
mProgressDialog.dismiss();
}
this.cancel(true);
super.onCancelled();
}
@Override
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setIcon(R.drawable.icon);
mProgressDialog.setTitle("附件下载");
mProgressDialog.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface Dialog, int keyCode, KeyEvent event) {
if(keyCode == event.KEYCODE_BACK) {
onCancelled();
}
return false;
}
});
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface Dialog, int whichButton) {
onCancelled();
}
});
mProgressDialog.show();
super.onPreExecute();
}
protected void onPostExecute(File apkUrl) {
if(mProgressDialog != null){
mProgressDialog.dismiss();
}
if(apkUrl != null){
Toast.makeText(context, "附件下载成功", Toast.LENGTH_LONG).show();
// Intent intent = new Intent();
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.setAction(android.content.Intent.ACTION_VIEW);
// Uri uri = Uri.fromFile(apkUrl);
// intent.setDataAndType(uri, "application/vnd.android.package-archive");
// context.startActivity(intent);
Utils.openFile(context, ServiceManagerApplication.filePath);
}else{
Toast.makeText(context, "服务器错误码:"+errorCode +"/n下载附件失败,请重试", Toast.LENGTH_SHORT).show();
}
}
}
* Object 下载地址
* Integer 进度
* File 下载完成后保存到File
*/
public class DownLoadFileTask extends AsyncTask<Object, Integer, File> {
private static final String TAG = "DownLoadFileTask";
private ProgressDialog mProgressDialog;
private Context context;
private String fileName;
private HttpURLConnection http;
private int errorCode;
// private String filePath;
public DownLoadFileTask(Context context){
this.context = context;
}
public DownLoadFileTask(Context context,String fileName){
this.context = context;
this.fileName = fileName;
}
protected File doInBackground(Object... views) {
String FileUrlStr = (String) views[0];
int count = 0;
int fileSize = 0;
File outFile = null;
try {
URL fileUrl = new URL(FileUrlStr);
http = (HttpURLConnection) fileUrl.openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Accept","image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
http.setRequestProperty("Accept-Language", "zh-CN");
http.setRequestProperty("Referer", fileUrl.toString());
http.setRequestProperty("Charset", "UTF-8");
http.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
http.setRequestProperty("Connection", "Keep-Alive");
Log.e(TAG, http.getResponseCode()+"========http.getResponseCode");
if(http.getResponseCode() == 200){
fileSize = http.getContentLength();
File sdCardDir = Utils.getSaveDir();
// Log.e(TAG, fileSize+"文件大小");
// Log.e(TAG, sdCardDir.getAbsolutePath());
if(!sdCardDir.exists()){
sdCardDir.mkdirs();
// Log.e(TAG, "创建附件保存文件夹成功");
}
if (fileSize <= 0) {
Log.e(TAG, "服务器地址错误!");
Toast.makeText(context, "附件大小为0,停止下载", Toast.LENGTH_SHORT).show();
return null;
}
mProgressDialog.setMax(fileSize);
InputStream inStream = http.getInputStream();
String contentEncodingStr = http.getContentEncoding();
// Log.i("DownLoadFileTask", "contentEncodingStr="+contentEncodingStr);
outFile = new File(sdCardDir , fileName);
ServiceManagerApplication.filePath = outFile.getAbsolutePath();
if("gzip".equalsIgnoreCase(contentEncodingStr)) {
GZIPInputStream inputGzipInputStream = new GZIPInputStream(inStream);
byte[] buffer = new byte[1024];
int len = -1;
FileOutputStream fos=new FileOutputStream(outFile);
while( (len = inputGzipInputStream.read(buffer)) != -1){
fos.write(buffer, 0, len);
count += len;
publishProgress(count);
}
fos.close();
inStream.close();
inputGzipInputStream.close();
} else {
byte[] buffer = new byte[1024];
int len = -1;
FileOutputStream fos=new FileOutputStream(outFile);
while( (len = inStream.read(buffer)) != -1){
fos.write(buffer, 0, len);
count += len;
publishProgress(count);
}
fos.close();
inStream.close();
}
}else{
Log.e(TAG, "服务器下载配置错误");
errorCode = http.getResponseCode();
// Toast.makeText(context, "服务器错误码:"+http.getResponseCode(), Toast.LENGTH_SHORT).show();
}
}catch (Exception e) {
e.printStackTrace();
return null;
}
return outFile;
}
@Override
protected void onProgressUpdate(Integer... values) {
mProgressDialog.setProgress(values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onCancelled() {
if(mProgressDialog != null){
mProgressDialog.dismiss();
}
this.cancel(true);
super.onCancelled();
}
@Override
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setIcon(R.drawable.icon);
mProgressDialog.setTitle("附件下载");
mProgressDialog.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface Dialog, int keyCode, KeyEvent event) {
if(keyCode == event.KEYCODE_BACK) {
onCancelled();
}
return false;
}
});
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface Dialog, int whichButton) {
onCancelled();
}
});
mProgressDialog.show();
super.onPreExecute();
}
protected void onPostExecute(File apkUrl) {
if(mProgressDialog != null){
mProgressDialog.dismiss();
}
if(apkUrl != null){
Toast.makeText(context, "附件下载成功", Toast.LENGTH_LONG).show();
// Intent intent = new Intent();
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.setAction(android.content.Intent.ACTION_VIEW);
// Uri uri = Uri.fromFile(apkUrl);
// intent.setDataAndType(uri, "application/vnd.android.package-archive");
// context.startActivity(intent);
Utils.openFile(context, ServiceManagerApplication.filePath);
}else{
Toast.makeText(context, "服务器错误码:"+errorCode +"/n下载附件失败,请重试", Toast.LENGTH_SHORT).show();
}
}
}