文件下载初级程序

[记录]文件下载初级程序

//程序很简单,入门好程序 mdf from《java多线程编程实战指南(核心篇)》
package com.example.demo.io.github.viscent.mtia.ch1;

import com.example.demo.io.github.viscent.mtia.util.Debug;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;

/**
 * 为每一个文件下载分配一个线程
 */
public class FileDownloaderAppTest {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //带下载的文件路径列表
        args = new String[]{"https://download.daokeyuedu.com/docbox/%E7%A8%BB%E5%A3%B3%E9%98%85%E8%AF%BB%E5%99%A8%E5%AE%89%E8%A3%85%E7%A8%8B%E5%BA%8F.exe"};

        String dest = "C:/Users/lan/Desktop";

        for (String arg : args) {
            String url = URLDecoder.decode(arg, "utf-8");

            //为每一个文件请求设置一个线程--启动线程
            new Thread(new DowloadFileR(url, dest)).start();
        }
        System.out.println("File Download Done!");

    }

    static class DowloadFileR implements Runnable {
        private String source;
        private String dest;

        public DowloadFileR(String source, String dest) {
            this.source = source;
            this.dest = dest;

        }

        ;

        @Override
        public void run() {
            try {
                this.dowloadFile(source, dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /**
         * 文件下载的具体逻辑
         */
        private void dowloadFile(String source, String dest) throws IOException {
            //文件名
            InputStream inputStream = null;
            HttpURLConnection urlConnection = null;
            String name = source.substring(source.lastIndexOf("/") + 1);
            dest = dest + "/" + name;
            try (RandomAccessFile randomAccessFile = new RandomAccessFile(dest, "rw")) {

                //建立连接
                URL url = new URL(source);
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");

                //验证返回值吧不为200和请求为空的情况
                if (2 != urlConnection.getResponseCode() / 100) {
                    throw new IOException("Request err: " + urlConnection.getResponseCode());
                }
                if (0 == urlConnection.getContentLength()) {
                    Debug.info("Nothing to be downloaded:" + source);
                    return;
                }
                ;

                //正常下载逻辑
                inputStream = urlConnection.getInputStream();
                ReadableByteChannel inChannel = Channels.newChannel(inputStream);
                FileChannel channel = randomAccessFile.getChannel();
                ByteBuffer buf = ByteBuffer.allocate(8192);
                while (-1 != inChannel.read(buf)) {
                    //读写切换
                    buf.flip();
                    //将缓冲区数据写入dest,清空缓冲区
                    channel.write(buf);
                    buf.clear();
                }
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }

            }
        }

        ;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值