文件下载

Posted nangongyibin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件下载相关的知识,希望对你有一定的参考价值。

文件下载

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

 

以上是关于文件下载的主要内容,如果未能解决你的问题,请参考以下文章

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途

代码片段 PHP,预期文件结尾,我错在哪里?

代码适用于与单个 html 文件不同的堆栈片段

VS code自定义用户代码片段snippet

Java itext为pdf 文件添加水印核心功能代码片段

VSCode自定义代码片段——声明函数