文件的下载

文件的下载注意两点:

1.首先要判断本地是否有这个文件,有就直接拿来用,没有再下载。

2.在完成下载的回调里面更新界面操作要放在主线程中。

 try {
            filesList = (List<FilesBean>) bundle.get("filesList");
            L.d(TAG,"我是传过来的文件集合-=="+filesList.toString());
            if (fileList!=null){
                fileHlv.setVisibility(View.VISIBLE);
                if (client == null) {
                    client = new AsyncHttpClient(6000);
                }
                DialogUtils.showProgressDia(mContext, "请稍等正在处理数据...", client);
                for (final FilesBean filesBean:filesList) {
                    L.d(TAG, "文件的路径-===" + filesBean.getFilesPath());
                    File file = new File(Constant.FILE_TEMP_PATH + filesBean.getFilesName());
                    if (!file.exists()) {
                    L.d(TAG,"我是文件下载,我被执行了");
                        HttpClientUtils hct = new HttpClientUtils(getString(R.string.interface_url_file) + filesBean.getFilesPath(), Constant.FILE_TEMP_PATH, filesBean.getFilesName()
                                , new HttpClientUtils.CallBack() {
                            @Override
                            public void isComplete(boolean b) {
                                if (b) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                fileList.add(Constant.FILE_TEMP_PATH + filesBean.getFilesName());
                                fileAdapter.notifyDataSetChanged();
                            }
                        });
                                }
                            }
                        });
                        hct.downloadFile();
                    }else {
                        fileList.add(Constant.FILE_TEMP_PATH + filesBean.getFilesName());
                        fileAdapter.notifyDataSetChanged();
                    }
                }

            }


        }catch (Exception e){
            e.printStackTrace();
            L.e("忽略错误");
        }
上面是当文件的集合传递过来所做的操作。

文件下载工具类:

/**
 * 文件下载utils
 * Created by JIANG on 2016/10/19.
 */
public class HttpClientUtils {
    private static final String TAG ="HttpClientUtils" ;
    private String urlPath ;
    private String outPath;
    private String fileName;
    private CallBack callBack;
    /**
     *
     * @param urlPath
     * @param outPath
     * @param fileName
     */
    public HttpClientUtils(String urlPath, String outPath, String fileName, CallBack callBack) {
        this.fileName = fileName;
        this.outPath = outPath;
        this.urlPath = urlPath;
        this.callBack = callBack;
    }
 public interface CallBack{
    void isComplete(boolean b);
}
    /**
     * 下载文件
     */
    public void downloadFile()
    {
        // 启动新线程下载软件
        new downloadFileThread().start();
    }
    /**
     * 下载文件线程
     */
    private class downloadFileThread extends Thread {
        @Override
        public void run() {
            try {
                // 判断SD卡是否存在,并且是否具有读写权限
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    // 获得存储卡的路径
                    //http://182.92.158.132/cloud/cloudfile?path=/public/notice/201606101737302282/16-131109141345 (1)_20160610173730880.jpg
                    //http://182.92.158.132/cloud/cloudfile?path=/public/notice/201606101737302282/16-131109141345%20(1)_20160610173730880.jpg
                    L.d(TAG,"我是路径-===="+urlPath);
                    URL url = new URL(urlPath);
                    // 创建连接
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.connect();
                    L.i("头:"+conn.getHeaderFields());;
                    // 获取文件大小
                    int length = conn.getContentLength();
                    L.i("length .." + length);
                    // 创建输入流
                    InputStream is = conn.getInputStream();

                    File file = new File(outPath);
                    // 判断文件目录是否存在
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    File apkFile = new File(outPath,fileName);
                    FileOutputStream fos = new FileOutputStream(apkFile);
                    int count = 0;
                    // 缓存
                    byte buf[] = new byte[1024];
                    // 写入到文件中

                        int numread = is.read(buf);
                        count += numread;
                        L.i("count .."+ count +"  numread .." + numread);

                        // 写入文件
                        fos.write(buf, 0, numread);
                    fos.close();
                    is.close();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            callBack.isComplete(true);

        }
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值