利用FinalHttp下载上传文件
2017-01-05 10:31
471 查看
类库下载地址:类库地址
下载:
以前分享过一个文件下载的工具类,不过那个很原始对于很多场景下运用不够方便,这次我分享一个专门用来下载文件的。
很简单就一段代码:
download方法参数:
第一个就是文件路径url,第二个参数是本地全路径,第三个是回调方法,回调的方法可以自己根据需求选择,也可以重写progress方法实现下载监听用来添加下载进度条。
上传:
带进度条的文件下载:
下载:
以前分享过一个文件下载的工具类,不过那个很原始对于很多场景下运用不够方便,这次我分享一个专门用来下载文件的。
很简单就一段代码:
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); } } }; }
相关文章推荐
- 利用ASIHttp实现文件的下载、上传和网络状态的监控
- 利用webservice和.net技术上传和下载文件--Sending files in chunks with MTOM Web Services and .NET 2.0 from:http://www.codeproject.com/soap/MTO
- 利用http实现文件的上传和下载
- 利用Indy的TIdFtp控件实现FTP协议, 远程下载上传文件
- 利用jspsmartupload组件来上传和下载文件
- [已上传demo]使用HttpHandler做文件过滤器,验证下载文件权限
- Symbian 真机http上传下载文件
- ASP.NET利用HttpHandler实现多扩展名文件下载
- 利用WebService实现远程服务器文件的上传和下载
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- JSP利用SmartUpload组件实现文件上传下载
- 利用Web/WCF Service上传下载文件[网上资源]
- (转)利用HttpWebRequest上传文件应该注意的问题
- 利用dorado mapping快速开发文件的上传和下载
- 远程下载http文件--利用WinInet类(转)
- VC利用http远程下载文件
- 利用API在FTP服务器上上传和下载文件
- Java Web 开发中文件的上传与下载(FTP/HTTP)
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载