码迷,mamicode.com
首页 > 其他好文 > 详细

文件下载

时间:2019-01-06 16:38:15      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:进度   ESS   down   链接   div   info   图片   etc   tin   

文件下载

  • 画UI 

技术分享图片

 

  • 根据UI写对应的逻辑
        new Thread(new Runnable() {
            @Override
            public void run() {
                String path = mEt.getText().toString().trim();
                if (TextUtils.isEmpty(path)) {
                    Toast.makeText(MainActivity.this, "下载链接地址不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    try {
                        URL url = new URL(path);
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        conn.setRequestMethod("GET");
                        conn.setConnectTimeout(5000);
                        File file = new File(getFileName(path) + ".txt");
                        String path1 = file.getPath();
                        Log.d(TAG, "run: "+path1);
                        boolean exists = file.exists();
                        Log.d(TAG, "run: "+exists);
                        if (file.exists() && file.length() > 0) {
                            FileInputStream fileInputStream = new FileInputStream(file);
                            BufferedReader bfis = new BufferedReader(new InputStreamReader(fileInputStream));
                            String lastDownloadPosition = bfis.readLine();
                            mStartIndex = Integer.parseInt(lastDownloadPosition);
                            conn.setRequestProperty("range","bytes="+mStartIndex+"-");
                            bfis.close();
                        }
                        int code = conn.getResponseCode();
                        Log.d(TAG, "run: "+code);
                        if (code ==200){
                            mLength = conn.getContentLength();
                        }
                        if (code == 200||code ==206) {
                            //设置进度条的最大进度
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mPb.setMax(mLength);
                                }
                            });
                            InputStream is = conn.getInputStream();
                            RandomAccessFile raf = new RandomAccessFile(getFileName(path),"rw");
                            long length = raf.length();
                            Log.d(TAG, "run: "+length);
                            raf.seek(mStartIndex);
                            int len = 0;
                            mCurrentsize = mStartIndex;
                            byte[] buf = new byte[1024];
                            while ((len = is.read(buf)) != -1) {
                                raf.write(buf, 0, len);
                                if (isstop) {
                                    RandomAccessFile raff = new RandomAccessFile(getFileName(path) + ".txt", "rw");
                                    raff.write(String.valueOf(mCurrentsize).getBytes());
                                    raff.close();
                                    break;
                                }
                                mCurrentsize += len;
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mPb.setProgress(mCurrentsize);
                                    }
                                });
                            }
                            is.close();
                            raf.close();
                            if (mCurrentsize==mLength){
                                File deleteFile = new File(getFileName(path) + ".txt");
                                deleteFile.delete();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(MainActivity.this, "下载成功", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

 

  • 记录当前线程下载的位置
if (isstop) {
                                    RandomAccessFile raff = new RandomAccessFile(getFileName(path) + ".txt", "rw");
                                    raff.write(String.valueOf(mCurrentsize).getBytes());
                                    raff.close();
                                    break;
                                }

 

  • 判断是否下载过 如果下载过继续上次的位置继续下
                        if (file.exists() && file.length() > 0) {
                            FileInputStream fileInputStream = new FileInputStream(file);
                            BufferedReader bfis = new BufferedReader(new InputStreamReader(fileInputStream));
                            String lastDownloadPosition = bfis.readLine();
                            mStartIndex = Integer.parseInt(lastDownloadPosition);
                            conn.setRequestProperty("range","bytes="+mStartIndex+"-");
                            bfis.close();
                        }

 

  • 判断文件是否下载完成 如果下载完成 把.txt文件删除
                            if (mCurrentsize==mLength){
                                File deleteFile = new File(getFileName(path) + ".txt");
                                deleteFile.delete();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(MainActivity.this, "下载成功", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }

 

文件下载

标签:进度   ESS   down   链接   div   info   图片   etc   tin   

原文地址:https://www.cnblogs.com/nangongyibin/p/10229009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!