网 上这类很多
你查一下下载,或断点续传 之类的
都会找到这些例子的

解决方案 »

  1.   

    import java.net.*;import java.io.*;
    public class FilesDownLoad 
    { public static void main (String args[]) 
    {
    String logo="http://www.google.com/images/hp0.gif";
    try 
    {

    URL root = new URL(logo);

    saveBinaryFile(root);//下载文件

    }

    catch (MalformedURLException e) 
    {

    System.err.println(logo + " 不是一个Http 类型的URL .");

    }

    catch (IOException e) 
    {

    System.err.println(e);

    }


    }
    public static void saveBinaryFile(URL u) throws IOException 
    {


    URLConnection uc = u.openConnection();

    String contentType = uc.getContentType();

    int contentLength = uc.getContentLength();//取得文件大小


    InputStream in = new BufferedInputStream(uc.getInputStream());


    String filename = u.getFile();

    //获取URL 文件名 如http://www.google.com/images/hp0.gif


    filename = filename.substring(filename.lastIndexOf('/') + 1);

    //去除http://www.google.com/images/


    FileOutputStream fout = new FileOutputStream(filename);//文件存在程序运行目录下


    byte[] data = new byte[1024];


    int bytesRead = 0;

    int bytesTotalRead=0;


    while (bytesTotalRead < contentLength)
    {

    bytesRead = in.read(data);//从输入流读数据到data中

    if (bytesRead == -1)

    break;

    else
    {

    fout.write(data); //写data数据到本地文件流

    bytesTotalRead += bytesRead;

    }

    }

    in.close();

    fout.flush();

    fout.close();

    if (bytesTotalRead != contentLength) 
    {

    throw new IOException("下载文件出错!");

    }

    }}