AsyncHttpClient 下载文件

来源:互联网 发布:php写api接口实例 编辑:程序博客网 时间:2024/06/07 04:43


public static AsyncHttpClient client = new AsyncHttpClient();

// 下载apk
public void downApk(final AppInfo info, final Context context) {
// 指定文件类型
String[] allowedContentTypes = new String[] { ".*" };
// 获取二进制数据如图片和其他文件
client.get(info.getAppUrl(), new BinaryHttpResponseHandler(
allowedContentTypes) {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] binaryData) {


// 文件夹地址
String tempPath = "Download";
// 文件地址
String filePath =  tempPath +"/"+ info.getAppName() + ".apk";
// 下载成功后需要做的工作
Log.e("binaryData:", "共下载了:" + binaryData.length);


FileUtils fileutils = new FileUtils();


// 判断sd卡上的文件夹是否存在
if (!fileutils.isFileExist(tempPath)) {
fileutils.createSDDir(tempPath);
}


// 删除已下载的apk
if (fileutils.isFileExist(filePath)) {
fileutils.deleteFile(filePath);
}


InputStream inputstream = new ByteArrayInputStream(binaryData);
if (inputstream != null) {
fileutils.write2SDFromInput(filePath, inputstream);
try {
inputstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}


}


@Override
public void onFailure(int statusCode, Header[] headers,
byte[] binaryData, Throwable error) {
Log.i("http", error.getMessage());
}


@Override
public void onProgress(int bytesWritten, int totalSize) {
// TODO Auto-generated method stub
super.onProgress(bytesWritten, totalSize);


int count = (int) ((bytesWritten * 1.0 / totalSize) * 100);


// 下载进度显示
// progress.setProgress(count);


Log.e("下载 Progress>>>>>", bytesWritten + " / " + totalSize);


}


@Override
public void onRetry(int retryNo) {
// TODO Auto-generated method stub
super.onRetry(retryNo);
// 返回重试次数
}


});
}




package com.htvmarket.utils;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.os.Environment;


public class FileUtils {
/**
* 需要知道当前SD卡的目录,Environment.getExternalStorageDierctory()
*/


private String SDPATH;


public String getSDPATH() {
return SDPATH;
}


public FileUtils() { // 目录名/sdcard
SDPATH = Environment.getExternalStorageDirectory() + "/";
}


// 在sdcard卡上创建文件
public File createSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}


// 在sd卡上创建目录
public File createSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
// mkdir只能创建一级目录 ,mkdirs可以创建多级目录
dir.mkdir();
return dir;
}


// 判断sd卡上的文件夹是否存在
public boolean isFileExist(String fileName) {
File file = new File(SDPATH + fileName);
return file.exists();
}


public void deleteFile(String fileName) {
File file = new File(SDPATH + fileName);
file.delete();
}


/**
* 将一个inputstream里面的数据写入SD卡中 第一个参数为目录名 第二个参数为文件名
*/
public File write2SDFromInput(String path, InputStream inputstream) {
File file = null;
OutputStream output = null;
try {
file = createSDFile(path);
output = new FileOutputStream(file);
// 4k为单位,每4K写一次
byte buffer[] = new byte[4 * 1024];
int temp = 0;
while ((temp = inputstream.read(buffer)) != -1) {
// 获取指定信,防止写入没用的信息
output.write(buffer, 0, temp);
}
output.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}


return file;
}
}


0 0
原创粉丝点击