import java.io.*;
import java.net.*;public class getWebContent {
public static String getWebCon(String domain){
System.out.println("开始读取内容...("+domain+")");
StringBuffer sb = new StringBuffer();
try{
URL url = new URL(domain);
File file=new File("file","1.html");
FileOutputStream fos=new FileOutputStream(file);
byte[] b=new byte[1024];
int len;
while((len= url.openStream().read(b))!=-1){
fos.write(b,0,len);
}
fos.close();
}catch(Exception e) { // Report any errors that arise
sb.append(e.toString());
System.err.println(e);
System.err.println("Usage: java HttpClient <URL> [<filename>]");
}
// System.out.println(sb);
return sb.toString();
}
}
这个静态方法我要实现复制网页源码的功能,可是复制是复制了,并没有完全复制下来,因为当将复制下来的文件保存为html文件时,打开只显示标题并不显示内容。用记事本打开对比时发现复制得并不完全,是不是有些会被屏蔽呢?我不知道遗漏了哪些地方。请大侠们多多指教
import java.net.*;public class getWebContent {
public static String getWebCon(String domain){
System.out.println("开始读取内容...("+domain+")");
StringBuffer sb = new StringBuffer();
try{
URL url = new URL(domain);
File file=new File("file","1.html");
FileOutputStream fos=new FileOutputStream(file);
byte[] b=new byte[1024];
int len;
while((len= url.openStream().read(b))!=-1){
fos.write(b,0,len);
}
fos.close();
}catch(Exception e) { // Report any errors that arise
sb.append(e.toString());
System.err.println(e);
System.err.println("Usage: java HttpClient <URL> [<filename>]");
}
// System.out.println(sb);
return sb.toString();
}
}
这个静态方法我要实现复制网页源码的功能,可是复制是复制了,并没有完全复制下来,因为当将复制下来的文件保存为html文件时,打开只显示标题并不显示内容。用记事本打开对比时发现复制得并不完全,是不是有些会被屏蔽呢?我不知道遗漏了哪些地方。请大侠们多多指教
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;public class URLParse {
// Java解析URL
public static void DownLoadPages(String urlStr, String outPath) {
URL url = null;
HttpURLConnection httpConn = null;
InputStream in = null;
FileOutputStream out = null; BufferedReader reader = null;
BufferedWriter writer = null;
try {
url = new URL(urlStr);
httpConn = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestMethod("GET");
in = httpConn.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
out = new FileOutputStream(new File(outPath));
writer = new BufferedWriter(new OutputStreamWriter(out));
String str = "";
while ((str = reader.readLine()) != null) {
System.out.println(str);
// writer.write(str);
// writer.newLine();
// writer.flush();
}
reader.close();
writer.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpConn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
DownLoadPages("http://news.baidu.com","d:/bb.txt");
}}