【Java基础之网络编程】代码库(七)

单线程下载.java

/**
 * FileOutputStream 类创建输出流对象, 然后使用 write() 方法, 将从输入流获得的网络资源保存到磁盘上, 实现网络资源的单线程下载.
**/

public void download(String urlAddr) {
    try {
        URL url = new URL(urlAddr); //创建 url 对象
        URLConnection urlConn = url.openConnection(); //打开 url 连接
        urlConn.connect(); //连接

        InputStream in = urlConn.getInputStream(); //获取字节流

        String filePath = url.getFile(); //获取完整路径
        int pos = filePath.lastIndexOf("/") //获得路径中最后一个斜杠的位置
        String fileName = filePath.substring(pos + 1); //截取文件名

        FileOutputStream out = new FileOutputStream("D:/" + fileName);//创建输出流对象
        byte[] bt = new byte[1024]; //声明存放下载内容的字节数组
        int len = in.read(bt); //从输入流中读取内容

        while(len != -1) {
            out.write(bt, 0, len); //将读取的内容全部写入到输出流
            len = in.read(bt); //继续从输入流中读取内容
        }

        //关闭资源
        out.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
//注意: 在网络下载时, 通常需要及时的关闭 I/O 流, 因为每个 I/O 流都会占用较多的系统资源, 且不能被垃圾回收机制回收, 当下载资源的用户较多时容易造成系统崩溃.

断点续传.java

public void download(long startPosition, long endPosition) {
    try {
        URL url = new URL(urlAddress);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();

        //设置请求属性和范围
        conn.setRequestProperty("User-Agent", "NetFox");
        String rangeProperty = "bytes=" + startPosition + "-";

        if (endPosition > 0) {
            rangeProperty += endPosition;
        }

        conn.setRequestProperty("RANGE", rangeProperty);
        conn.connect();

        InputStream in = conn.getInputStream();
        String file = url.getFile();
        String name = file.substring(file.lastIndexOf("/") + 1);
        FileOutputStream out = new FileOutputStream("D:/" + name, true);

        byte[] buff = new byte[2048];
        int len = 0;
        len = in.read(buff);

        while (len != -1) {
            out.write(buff, 0, len);
            len = in.read(buff);
        }

        out.close();
        in.close();
        conn.disconnect();

        if ( readToPos > 0 && readToPos == totalLength ) {
            System.exit(0);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

多线程下载.java

public class DownloadMultiThread implements Runnable {
    private String sURL = null;
    private File desFile;
    private long startPos;
    private long endPos;

    public DownloadMultiThread() {

    }

    public DownloadMultiThread(String sURL, File desFile, long startPos, long endPos) {
        this.sURL = sURL;
        this.desFile = desFile;
        this.startPos = startPos;
        this.endPos = endPos;
    }

    public void run() {
        RandomAccessFile out = new RandomAccessFile(desFile, "rw");
        out.seek(startPos);
        InputStream in = conn.getInputStream();
        BufferedInputStream bin = new BufferedInputStream(in);
        byte[] buff = new byte[2048];
        int len = -1;

        //读取到内存并添加到字节数组
        len = bin.read(buff);
        while (len != -1) {
            out.write(buff, 0, len);
            len = bin.read(buff);
        }
    }
}


public void download(String url, String dest, int threadNum) throws Exception {
    URL downURL = new URL(url);
    HttpURLConnection conn = (HttpURLConnection)downURL.openConnection(); //打开网络连接
    long fileLength = -1;
    int stateFlagCode = conn.getResponseCode(); //获得连续状态标记代码

    //网络连接正常
    if (stateFlagCode == 200) {
        fileLength = conn.getContentLength(); //获得文件的长度
        conn.disconnect();
    }

    if (fileLength > 0) {
        long byteCounts = fileLength / threadNum + 1; //计算每个线程的字节数
        File file = new File(dest);
        int i = 0;
        while (i < threadNum) {
            //定义开始和结束位置
            long startPosition = byteCounts * i;
            long endPosition = byteCounts * (i + 1);
            if (i == threadNum -1) {
                /* 创建 DownloadMultiThread 线程的实例 */
                DownloadMultiThread fileThread = new DownloadMultiThread(url, file, startPosition, 0); 
                new Thread(fileThread).start(); //启动一个线程对象
            } else {
                DownloadMultiThread fileThread = new DownloadMultiThread(url, file, startPosition, endPosition); 
                new Thread(fileThread).start(); //启动一个线程对象
            }
            i++;
        }
        JOptionPane.showMessageDialog(null, "完成网络资源下载");
    }
}
//注意: 使用多线程下载程序时, 由于创建线程是非常消耗资源的, 如果线程的创建较多, 将极大的影响系统性能, 这时可以使用线程池来提高系统系能, 当需要创建线程时, 可以从线程池中取出空闲线程, 从而提高多线程下载的系统性能.

获取网页内容.java

public Collection<String> getURLCollection(String urlString) {
    URL url = null;
    URLConnection conn = null;

    Collection<String> urlCollection = new ArrayList<String>();
    try {
        url = new URL();
        conn = url.openConnection();
        conn.connect();

        InputStream is = conn.getInputStream();
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        String nextLine = br.readLine();

        //遍历读取网页的全部内容, 并添加到集合对象中
        while (nextLine != null) {
            urlCollection.add(nextLine);
            nextLine = br.readLine();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return urlCollection;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值