您的位置:首页 > 理论基础 > 计算机网络

利用FinalHttp下载上传文件

2017-01-05 10:31 471 查看
类库下载地址:类库地址



下载:

以前分享过一个文件下载的工具类,不过那个很原始对于很多场景下运用不够方便,这次我分享一个专门用来下载文件的。

很简单就一段代码:

FinalHttp finalHttp = new FinalHttp();
finalHttp.download(getString(R.string.interface_url_file) + imgBean.getTxt(), Constant.FILE_TEMP_PATH + imgBean.getId(),
new AjaxCallBack<File>() {
@Override
public void onSuccess(File file) {
super.onSuccess(file);
DialogUtils.closeProgressDia();
fileList.add(Constant.FILE_TEMP_PATH + imgBean.getId());
fileHlv.setVisibility(View.VISIBLE);
fileAdapter.notifyDataSetChanged();
}

@Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
super.onFailure(t, errorNo, strMsg);
DialogUtils.closeProgressDia();
}
});


download方法参数:

第一个就是文件路径url,第二个参数是本地全路径,第三个是回调方法,回调的方法可以自己根据需求选择,也可以重写progress方法实现下载监听用来添加下载进度条。

上传:

AjaxParams params = new AjaxParams();
params.put("username", "michael yang");
params.put("password", "123456");
params.put("email", "test@tsz.net");
params.put("profile_picture", new File("/mnt/sdcard/pic.jpg")); // 上传文件
params.put("profile_picture2", inputStream); // 上传数据流
params.put("profile_picture3", new ByteArrayInputStream(bytes)); // 提交字节流
FinalHttp fh = new FinalHttp();
fh.post("http://www.yangfuhai.com", params, new AjaxCallBack(){ @Override public void onLoading(long count, long current) { textView.setText(current+"/"+count); }
@Override public void onSuccess(String t) {
textView.setText(t==null?"null":t);
} });
主要就是利用post上传其中AjaxParams就是参数对象类型可以是Map<String,String> 也可以是Map<String,File>等

带进度条的文件下载:

package cn.com.bjhj.utils.finalfileprogress;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import java.io.File;

import cn.com.bjhj.activity.R;
import cn.com.bjhj.activity.base.HHYBaseActivity;
import cn.com.bjhj.utils.HHYFileUtils;
import cn.com.bjhj.utils.L;

/**
* 类介绍(必填):自定义文件下载附带progressbar
* Created by Jiang on 2017/1/6 14:24.
*/

public class FinalDownFile  {
private static final java.lang.String TAG = "自定义文件下载";
private HHYBaseActivity mActivity;
private ProgressBar mProgress;
private boolean cancelUpdate;
private AlertDialog mDownloadDialog;
private String strPath;
private String outPath;
private int rateNum;
private DownFileCallBack callBack;

public FinalDownFile(HHYBaseActivity context, String strPath, String outPath,DownFileCallBack callBack) {
this.mActivity = context;
this.strPath = strPath;
this.outPath = outPath;
this.callBack = callBack;
}
public interface DownFileCallBack{
void onSuccess(File file);
void onFailure(String err);
}

public void showDownloadDialog() {
// 构造软件下载对话框
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
builder.setTitle(R.string.soft_updating);
// 给下载对话框增加进度条
final LayoutInflater inflater = LayoutInflater.from(mActivity);
View v = inflater.inflate(R.layout.dialog_softupdate_progress, null);
mProgress = (ProgressBar) v.findViewById(R.id.update_progress);

builder.setView(v);
// 取消下载
builder.setNegativeButton(R.string.soft_update_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();

}
});
mDownloadDialog = builder.create();
mDownloadDialog.setCanceledOnTouchOutside(false);
mDownloadDialog.show();

// 下载文件
downloadFile();
}

private void downloadFile() {
FinalHttp finalHttp = new FinalHttp();
//中文转utf-8
strPath = strPath.replace(HHYFileUtils.getFileName(strPath), Uri.encode(HHYFileUtils.getFileName(strPath)));
finalHttp.download(strPath, outPath, new AjaxCallBack<File>() {
@Override
public void onLoading(long count, long current) {
//                super.onLoading(count, current);
mProgress.setMax((int) count);
rateNum = (int) current;
handler.sendEmptyMessage(0);
L.d(TAG,"我是下载进度-=="+count+"\n"+current);
}

@Override
public void onSuccess(File file) {
super.onSuccess(file);
L.d(TAG,"下载成功-==");
mDownloadDialog.dismiss();
callBack.onSuccess(file);
}

@Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
super.onFailure(t, errorNo, strMsg);
L.i(TAG,"下载失败-=="+strMsg);
mDownloadDialog.dismiss();
callBack.onFailure(strMsg);
}
});
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);

if(mActivity != null){
mActivity.onProgress(mProgress, rateNum);
}

}
};

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  文件下载