首页 > 代码库 > 网络下载器

网络下载器

网络下载器:

public static void main(String[] args) {
            OutputStream  download=null;
            HttpURLConnection conn=null;
            try {
                URL    url=new URL
("http://dlc2.pconline.com.cn/filedown_171040_8417060/Yx2PSEuB/jpwb2017cl.exe");
                conn=(HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(3000);
                 System.out.println("响应码:"+conn.getResponseCode());
                if(conn.getResponseCode()==200){
                    InputStream  readInfo=conn.getInputStream();
                    download=new FileOutputStream("f:\\jpwb2.exe");
                    byte[]  data=http://www.mamicode.com/new byte[1024*100];
                    int len=0;
                    
                    while( (len=readInfo.read(data))!=-1){
                        System.out.println("从服务器上面读取了"+(len/1024.0)+"KB的数据");
                        download.write(data, 0, len);
                        download.flush();
                    }
                    System.out.println("-----文件下载成功----");
                }
            } catch (MalformedURLException e) {             
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(download!=null){
                    try {
                        download.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(conn!=null){
                    conn.disconnect();                }
            }
        }

 

网络下载器