文件下载显示进度条

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

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

  

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的控制,这个最近正在研究,应该会很简单,做好之后再添加到这篇博客中,咱们共同分享。

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值