博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件下载
阅读量:6487 次
发布时间:2019-06-24

本文共 5769 字,大约阅读时间需要 19 分钟。

文件下载

  • 画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();                                    }                                });                            }

 

转载于:https://www.cnblogs.com/nangongyibin/p/10229009.html

你可能感兴趣的文章
Android Studio插件
查看>>
java中的访问权限
查看>>
CallableAndFuture
查看>>
Nginx配置文件详细说明
查看>>
开源爬虫larbin分析
查看>>
C# Linq获取两个List或数组的差集交集
查看>>
YUV422转RGB24
查看>>
数据库:mongodb与关系型数据库相比的优缺点zz
查看>>
webclinet downstring 搜狐 为什么是个?号
查看>>
new的三种形态
查看>>
记录发现的一个hiredis的bug
查看>>
WPF整理-使用ResourceDictionary管理Logical Resources
查看>>
广告banner:手动滑动切换,自动切换,点击跳转,异步加载网络图片
查看>>
2018-05-17 第十一天
查看>>
linux 下实现ssh免密钥登录
查看>>
MySQL 5.7 LOGICAL_CLOCK 并行复制原理及实现分析
查看>>
apache访问日志
查看>>
shell后台执行命令-crontab
查看>>
jQuery |添加标签元素/内容
查看>>
oracle 动态注册和静态注册
查看>>