AsyncHttpClient 下载文件

 

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;
}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
java 工具async-http-client-1.6.3.jar com.google.gson.DefaultDateTypeAdapter.class com.google.gson.ExclusionStrategy.class com.google.gson.FieldAttributes.class com.google.gson.FieldNamingPolicy.class com.google.gson.FieldNamingStrategy.class com.google.gson.Gson.class com.google.gson.GsonBuilder.class com.google.gson.InstanceCreator.class com.google.gson.JsonArray.class com.google.gson.JsonDeserializationContext.class com.google.gson.JsonDeserializer.class com.google.gson.JsonElement.class com.google.gson.JsonIOException.class com.google.gson.JsonNull.class com.google.gson.JsonObject.class com.google.gson.JsonParseException.class com.google.gson.JsonParser.class com.google.gson.JsonPrimitive.class com.google.gson.JsonSerializationContext.class com.google.gson.JsonSerializer.class com.google.gson.JsonStreamParser.class com.google.gson.JsonSyntaxException.class com.google.gson.LongSerializationPolicy.class com.google.gson.TreeTypeAdapter.class com.google.gson.TypeAdapter.class com.google.gson.TypeAdapterFactory.class com.google.gson.annotations.Expose.class com.google.gson.annotations.SerializedName.class com.google.gson.annotations.Since.class com.google.gson.annotations.Until.class com.google.gson.internal.ConstructorConstructor.class com.google.gson.internal.Excluder.class com.google.gson.internal.JsonReaderInternalAccess.class com.google.gson.internal.LazilyParsedNumber.class com.google.gson.internal.ObjectConstructor.class com.google.gson.internal.Primitives.class com.google.gson.internal.Streams.class com.google.gson.internal.StringMap.class com.google.gson.internal.UnsafeAllocator.class com.google.gson.internal.bind.ArrayTypeAdapter.class com.google.gson.internal.bind.CollectionTypeAdapterFactory.class com.google.gson.internal.bind.DateTypeAdapter.class com.google.gson.internal.bind.JsonTreeReader.class com.google.gson.internal.bind.JsonTreeWriter.class com.google.gson.internal.bind.MapTypeAdapterFactory.class com.google.gson.internal.bind.ObjectTypeAdapter.c

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值