网络编程之下载

来源:互联网 发布:java 图片加密 编辑:程序博客网 时间:2024/06/09 23:53

1、

package com.eduask.socketDeom;import java.io.InputStream;import java.io.RandomAccessFile;import java.net.*;public class DownUtil{    private String path;    private String targetFile;    private int threadNum;    private DownThread[] threads;    private int fileSize;    public DownUtil(String path, String targetFile, int threadNum)    {        this.path = path;        this.threadNum = threadNum;        threads = new DownThread[threadNum];        this.targetFile = targetFile;    }    public void download() throws Exception    {        //得到一个Ulr        URL url = new URL(path);        //根据url打开一个网络连接        HttpURLConnection conn = (HttpURLConnection) url.openConnection();        //设置超时时间为5S        conn.setConnectTimeout(5 * 1000);        //设置网络的请求方式为get请求方式        conn.setRequestMethod("GET");        //设置可以请求那些资源        conn.setRequestProperty(            "Accept",            "image/gif, image/jpeg, image/pjpeg, image/pjpeg, "            + "application/x-shockwave-flash, application/xaml+xml, "            + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "            + "application/x-ms-application, application/vnd.ms-excel, "            + "application/vnd.ms-powerpoint, application/msword, */*");        //设置接收的请求语言是中文        conn.setRequestProperty("Accept-Language", "zh-CN");        //设置字符的编码方式是utf-8        conn.setRequestProperty("Charset", "UTF-8");        //设置一直保持连接        conn.setRequestProperty("Connection", "Keep-Alive");        //得到资源的大小        fileSize = conn.getContentLength();        //服务器不会有其他的请求        conn.disconnect();        //每个线程加载多大的内容        int currentPartSize = fileSize / threadNum + 1;        //以可读客写的方式打开了一个随机读写流        RandomAccessFile file = new RandomAccessFile(targetFile, "rw");        //设置大小是文件的大小        file.setLength(fileSize);        file.close();        for (int i = 0; i < threadNum; i++)        {            //设置RandomAcccessFile的偏移量            int startPos = i * currentPartSize;            RandomAccessFile currentPart = new RandomAccessFile(targetFile,                "rw");            currentPart.seek(startPos);            threads[i] = new DownThread(startPos, currentPartSize,                currentPart);            threads[i].start();        }    }    public double getCompleteRate()    {        int sumSize = 0;        for (int i = 0; i < threadNum; i++)        {            sumSize += threads[i].length;        }        return sumSize * 1.0 / fileSize;    }    private class DownThread extends Thread    {        private int startPos;        private int currentPartSize;        private RandomAccessFile currentPart;        public int length;        public DownThread(int startPos, int currentPartSize,            RandomAccessFile currentPart)        {            this.startPos = startPos;            this.currentPartSize = currentPartSize;            this.currentPart = currentPart;        }        @Override        public void run()        {             try            {                URL url = new URL(path);                HttpURLConnection conn = (HttpURLConnection)url                    .openConnection();                conn.setConnectTimeout(5 * 1000);                conn.setRequestMethod("GET");                conn.setRequestProperty(                    "Accept",                    "image/gif, image/jpeg, image/pjpeg, image/pjpeg, "                    + "application/x-shockwave-flash, application/xaml+xml, "                    + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "                    + "application/x-ms-application, application/vnd.ms-excel, "                    + "application/vnd.ms-powerpoint, application/msword, */*");                conn.setRequestProperty("Accept-Language", "zh-CN");                conn.setRequestProperty("Charset", "UTF-8");                InputStream inStream = conn.getInputStream();                inStream.skip(this.startPos);                byte[] buffer = new byte[1024];                int hasRead = 0;                while (length < currentPartSize                    && (hasRead = inStream.read(buffer)) != -1)                {                    currentPart.write(buffer, 0, hasRead);                    length += hasRead;                }                currentPart.close();                inStream.close();            }            catch (Exception e)            {                e.printStackTrace();            }        }    }}
0 0
原创粉丝点击