利用FinalHttp下载上传文件

类库下载地址:类库地址

下载:

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

很简单就一段代码:


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

        }
    };

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值