文件下载(下载进度监听)

package com.jdsoft.studio.util;

import com.jdsoft.util.UnzipUtility;
import org.apache.commons.fileupload.util.Streams;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * 文件下载Util
 */
public abstract class RemoteDownload {

    final static org.slf4j.Logger logger= LoggerFactory.getLogger(RemoteDownload.class);
    /**
     * @param path:文件保存的路径。文件路径
     * @param fileurl:文件的url
     * @param flag:true。文件显示在当前保存的路径下,false。文件显示在文件解压后的路径下
     */
    public void  downloadFile(File path,String fileurl,boolean flag){
        try {
            URL url = new URL(fileurl);
            InputStream is = url.openStream();
            final long total = url.openConnection().getContentLength();
            File dir = path;
            if(!dir.exists()){
                dir.mkdirs();
            }
            String fileName = fileurl.substring(fileurl.lastIndexOf("/"));
            File file = new File(dir,fileName);
            byte[] buf = new byte[1024*8];
            int len = 0;
            FileOutputStream fos = null;
            try{
                long sum = 0;
                fos = new FileOutputStream(file);
                while ((len = is.read(buf)) != -1){
                    sum += len;
                    fos.write(buf, 0, len);
                    final long finalSum = sum;
                    onProgress(finalSum * 100.0f / total, total);
                }
                fos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
                try{
                    if (is != null) is.close();
                } catch (IOException e){
                }
                try{
                    Streams.copy(is, fos, true);
                } catch (IOException e){
                }
            }
            File zipFilePath = new File(dir.toString()+"/"+fileName);
            String zipFileName = fileName.substring(0,fileName.lastIndexOf("."));
            File destDirectory = new File(dir.toString()+"/"+zipFileName);
            if(flag){
                destDirectory = new File(dir.toString());
            }
            logger.info("解压路径:"+destDirectory);
            UnzipUtility.unzip(zipFilePath.toString(),destDirectory.toString());
            logger.info("解压完成");
        } catch (Exception e) {
            logger.error(e.toString());
        }
    }

    /**
     * 下载进度监听
     * @param progress
     * @param total
     */
    public abstract void onProgress(float progress, long total);
}

使用:

static void main(String[] args) {
        String url = "http://127.0.0.1:8081/files/download.zip";
        File targetFile = new File(".", "ide");
        new RemoteDownload() {
            @Override
            void onProgress(float progress, long total) {
                System.out.println(progress);
            }
        }.downloadFile(targetFile, url, true);
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容