49 lines
1.5 KiB
Java
49 lines
1.5 KiB
Java
package com.util;
|
|
|
|
import java.io.BufferedInputStream;
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.InputStream;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
|
|
import android.app.ProgressDialog;
|
|
import android.content.Context;
|
|
|
|
public class DownloadManager {
|
|
public static File getFileFromServer(String path, ProgressDialog pd,Context context)
|
|
throws Exception {
|
|
// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
|
|
// if (Environment.getExternalStorageState().equals(
|
|
// Environment.MEDIA_MOUNTED)) {
|
|
URL url = new URL(path);
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
conn.setConnectTimeout(5000);
|
|
// 获取到文件的大小
|
|
pd.setMax(conn.getContentLength()/1000);
|
|
InputStream is = conn.getInputStream();
|
|
// File file = new File(Environment.getExternalStorageDirectory(),
|
|
// "updata.apk");//得到sd卡的目录
|
|
File file = new File(context.getCacheDir().getParentFile(),
|
|
"updata.apk"); //放到当前应用程序所在的目录下
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
BufferedInputStream bis = new BufferedInputStream(is);
|
|
byte[] buffer = new byte[1024];
|
|
int len;
|
|
int total = 0;
|
|
while ((len = bis.read(buffer)) != -1) {
|
|
fos.write(buffer, 0, len);
|
|
total += len;
|
|
// 获取当前下载量
|
|
pd.setProgress(total/1000);
|
|
}
|
|
fos.close();
|
|
bis.close();
|
|
is.close();
|
|
return file;
|
|
// } else {
|
|
// return null;
|
|
// }
|
|
}
|
|
}
|