//Download by HTTP
//For other protocol, do it yourself
import java.net.*;
import java.io.*;
public class DownLoad
{
public static void main(String args[])
{
DownLoad dl =new DownLoad();
String downfile ="http://moviesky.8u8.com/cool.htm";  // can be any file type! Not only htm files can be downloaded.
String saveplace ="c:\\test111.htm";
if(dl.downLoadFile(downfile,saveplace))
System.out.println("Download ok");
}
public boolean downLoadFile(String fileurl, String savepath)
{
try
{
URL  url =new URL(fileurl);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
int filelen = conn.getContentLength();
byte[] filebuf =new byte[filelen];
byte[] tmpbuf =new byte[1024];
int readcount =0;
int readnum = 0;
while(readcount<filelen&&readnum!=-1)
{
readnum=is.read(tmpbuf);
if(readnum>-1)
{
System.arraycopy(tmpbuf,0,filebuf,readcount,readnum);
readcount =readcount +readnum;
}
}
if(readcount<filelen)
{
System.out.println("download error");
return false;
}
File savefile =new File(savepath);
if(!savefile.exists())
savefile.createNewFile();
FileOutputStream fos = new FileOutputStream(savefile);
fos.write(filebuf);
fos.close(); }
catch(Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
}

解决方案 »

  1.   

    guojun_2000_2000,大虾请问您有更好的程序吗?
      

  2.   

    确实有点好笑,不过不要这么讽刺人家啊
    我不知道有没有更好的方法,但是你这段程序看起来不像java,有点不伦不类
      

  3.   

    public boolean downLoadFile2(String fileurl, String savepath)
    {//Things need to do:
     //Set connection timeout
     //Try more times to download
     //Catch a general Exception is not so good
     //...
    try
    {
    int httpStatusCode;
    URL  url =new URL(fileurl);
    URLConnection conn = url.openConnection();
    conn.connect();
    HttpURLConnection httpconn =(HttpURLConnection)conn;
    httpStatusCode =httpconn.getResponseCode();
    if(httpStatusCode!=HttpURLConnection.HTTP_OK)
    {//HttpURLConnection return an error code
    System.out.println("Connect to "+fileurl+" failed,return code:"+httpStatusCode);
    return false;
    }
    int filelen = conn.getContentLength();
    InputStream is = conn.getInputStream();
    byte[] tmpbuf=new byte[1024];
    File savefile =new File(savepath);
    if(!savefile.exists())
    savefile.createNewFile();
    FileOutputStream fos = new FileOutputStream(savefile);
    int readnum = 0;
    if(filelen<0)//for http://www.csdn.net/expert/topic/204/204361.shtm, conn.getContentLength() return -1.
    {
    while(readnum>-1)
    {
    readnum = is.read(tmpbuf);
    if(readnum>0)
    fos.write(tmpbuf,0,readnum);
    }
    }
    else
    {
    int readcount =0;
    while(readcount<filelen&&readnum!=-1)
    {
    readnum=is.read(tmpbuf);
    if(readnum>-1)
    {
    fos.write(tmpbuf,0,readcount);
    readcount =readcount +readnum;
    }
    }
    if(readcount<filelen)
    {
    System.out.println("download error");
    is.close();
    fos.close();
    savefile.delete();
    return false;
    }
    }
    fos.flush();
    fos.close();
    is.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    return false;
    }
    return true;
    }