我已经知道了网页的地址,希望把网页的代码下载后并保存在本地的html文件。
如何实现?谢谢

解决方案 »

  1.   

    我只知道把网页的代码下来:查看-》源文件
    至于下到HTML就不知道了
      

  2.   

    public  String getHTML(String url)//获取指定URL的网页,返回网页内容的字符串,然后将此字符串存到文件即可
    {
            try{
    URL newUrl=new URL(url);
    URLConnection connect=newUrl.openConnection();
                            connect.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
    DataInputStream dis=new DataInputStream(connect.getInputStream());
    BufferedReader in = new BufferedReader(new InputStreamReader(dis,encode));
    String html="";
    String readLine=null;
    while((readLine=in.readLine())!=null)
    {
    html=html+readLine;
    }
    in.close();
    return html;
    }catch(MalformedURLException me){
    System.out.println("MalformedURLException"+me);
    }
    catch(IOException ioe){
    System.out.println("ioeException"+ioe);
    }
    return null;

    }
      

  3.   

    FileOutputStream fos = new FileOutputSteam("d:\\new.html");
    URL url = new URL("http://bbs.csdn.net/");
    UrlConnection uc = url.openConnection();
    uc.connect();
    uc.setDoInput(true);                              
    uc.setDoOutput(false);  
    InputStream is = new InputStream(uc.getInputStream());
    byte[] buffer = new byte[2048];
    int length=0;
    while((length = is.read(buffer))!=-1)
    {  fos.write(buffer,0,length);      }
    is.close();
    fos.close();
      

  4.   

    URL url = new URL(urlString);
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    InputStream is = connection.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));再使用 BufferedReader 读取就可以了。
      

  5.   

    init:
    deps-jar:
    Compiling 1 source file to E:\Temp\java\catchweb\build\classes
    E:\Temp\java\catchweb\src\catchweb\Main.java:31: 找不到符号
    符号: 类 FileOutputSteam
    位置: 类 catchweb.Main
            FileOutputSteam fos = new FileOutputSteam("d:\\new.html");
    E:\Temp\java\catchweb\src\catchweb\Main.java:31: 找不到符号
    符号: 类 FileOutputSteam
    位置: 类 catchweb.Main
            FileOutputSteam fos = new FileOutputSteam("d:\\new.html");
    2 错误
    生成失败(总时间:0 秒)怎么办?
      

  6.   

    SORRY,写漏了
    FileOutputSteam fos = new FileOutputSteam("d:\\new.html"); 
    改为
    FileOutputStream fos = new FileOutputStream("d:\\new.html"); 
      

  7.   

    import java.net.*;
    import java.io.*;
    import java.util.regex.*;public class UrlT { public static void main(String[] args) {
    // TODO 自动生成方法存根
    try {
    URL url = new URL("http://www.ah.xinhuanet.com");
    BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));
    FileWriter wfile=new FileWriter("a.html");
    String str;
    while((str=br.readLine())!=null)
    wfile.write(str);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }}这个只是下载了html,如果你要图片的话,解析url,找到图片路径,下载保存