首页 > 学院 > 开发设计 > 正文

文件下载显示进度条

2019-11-06 07:01:45
字体:
来源:转载
供稿:网友

         文件下载网络上下载资源,当文件比较小的时候,没有显示进度,可能看不出来什么,但当文件内容比较大,显示出进度条,这样就更加贴切的让用户感到文件下载的状况。

     现在先做一个小例子,主要是现在控制台输出文件下载的进度。

  

package com.tgb.demo;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.RandomaccessFile;import java.net.HttpURLConnection;import java.net.URL;/** * Created by zss on 2017/3/5. */public class download {    public static void dowanload(String url, String path)            throws IOException {        System.out.PRintln("下载中...");        InputStream inputStream = null;        RandomAccessFile randomAccessFile = null;        try {            HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();            urlConnection.setRequestMethod("GET");            urlConnection.setConnectTimeout(10 * 1000);            File file = new File(path);            //文件夹是否存在            if (!file.getParentFile().exists())                file.getParentFile().mkdir();            if (file.exists())                file.delete();            file.createNewFile();            int responseCode = urlConnection.getResponseCode();            if (responseCode >= 200 && responseCode < 300) {                inputStream = urlConnection.getInputStream();                int len = 0;                byte[] data = new byte[4096];                //用于保存当前进度(具体进度)                int progres = 0;                //获取文件长度                int maxProgres = urlConnection.getContentLength();                randomAccessFile = new RandomAccessFile(file, "rwd");                //设置文件大小                randomAccessFile.setLength(maxProgres);                //将文件大小分成100分,每一分的大小为unit                int unit = maxProgres / 100;                //用于保存当前进度(1~100%)                int unitProgress = 0;                while (-1 != (len = inputStream.read(data))) {                    randomAccessFile.write(data, 0, len);                    progres += len;//保存当前具体进度                    int temp = progres / unit; //计算当前百分比进度                    if (temp >= 1 && temp > unitProgress) {//如果下载过程出现百分比变化                        unitProgress = temp;//保存当前百分比                        System.out.println("正在下载中..." + unitProgress + "%");                    }                }                inputStream.close();                System.out.println("下载完成...");            } else {                System.out.println("服务器异常...");            }        } finally {            if (null != inputStream) {                inputStream.close();            }            if (null != randomAccessFile) {                randomAccessFile.close();            }        }    }    public static void main(String[] args) throws IOException {        String path = "D://abc//image.jpg";        String url="http://www.dowei.com/d/file/tuku/meinv/2016-01-26/1453788622507000.jpg";        dowanload(url, path);    }}

       下面是显示的效果:

             

      如果想把这个效果做到前端,还需要前台js的控制,这个最近正在研究,应该会很简单,做好之后再添加到这篇博客中,咱们共同分享。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表