求java读取网页代码的方法 我已经知道了网页的地址,希望把网页的代码下载后并保存在本地的html文件。如何实现?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我只知道把网页的代码下来:查看-》源文件至于下到HTML就不知道了 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; } 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(); URL url = new URL(urlString);HttpURLConnection connection = (HttpURLConnection)url.openConnection();InputStream is = connection.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));再使用 BufferedReader 读取就可以了。 init:deps-jar:Compiling 1 source file to E:\Temp\java\catchweb\build\classesE:\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 秒)怎么办? SORRY,写漏了FileOutputSteam fos = new FileOutputSteam("d:\\new.html"); 改为FileOutputStream fos = new FileOutputStream("d:\\new.html"); 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,找到图片路径,下载保存 自动更新控件的问题 java编译器是什么编译出来的? 关于Java调用dll的一个问题. 在我电脑上可以运行,在其他电脑上就不行了 有关IO流字符编码的问题? JAVA 问题 大家来讨论下 Unix下weblogic里用JFreeChart 一个疑问!!!请各位高手帮忙!!! 线程优先级的问题,如何理解如下程序的运行结果??? 如何在JAVA方法中返回多个对象,谁能解决给200分。 黑豹的Java论坛在哪?Java教程在哪? 关于流问题 关于表格大小问题
至于下到HTML就不知道了
{
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;
}
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();
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));再使用 BufferedReader 读取就可以了。
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 秒)怎么办?
FileOutputSteam fos = new FileOutputSteam("d:\\new.html");
改为
FileOutputStream fos = new FileOutputStream("d:\\new.html");
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,找到图片路径,下载保存