09-06
11
远程采集,无乱码
作者:Java伴侣 日期:2009-06-11
下面是读取csdn中某一页的内容:
复制内容到剪贴板 程序代码
import java.net.*;
import java.io.*;
import java.util.*;
public class URLDemo {
public static void main(String[] args) {
System.out.println("Starting...");
int c;
URLConnection urlcon=null;
//HttpURLConnection urlcon = null;
try
{
URL url = new URL("http://www.csdn.net");
try
{
urlcon = url.openConnection();
//urlcon = (HttpURLConnection)url.openConnection();
}
catch (IOException e)
{
}
System.out.println("the date is :"+new Date(urlcon.getDate()));
System.out.println("content_type :"+urlcon.getContentType());
try
{
String webpage;
InputStream in = urlcon.getInputStream();
while((( c=in.read())!=-1))
{
int all=in.available();
byte[] b =new byte[all];
in.read(b);
webpage =new String(b,"UTF-8");
System.out.println(webpage);
}
in.close();
}
catch(IOException e)
{
System.out.println(""+e);
}
}
catch(MalformedURLException e)
{
System.out.println(""+e);
}
}
}
import java.io.*;
import java.util.*;
public class URLDemo {
public static void main(String[] args) {
System.out.println("Starting...");
int c;
URLConnection urlcon=null;
//HttpURLConnection urlcon = null;
try
{
URL url = new URL("http://www.csdn.net");
try
{
urlcon = url.openConnection();
//urlcon = (HttpURLConnection)url.openConnection();
}
catch (IOException e)
{
}
System.out.println("the date is :"+new Date(urlcon.getDate()));
System.out.println("content_type :"+urlcon.getContentType());
try
{
String webpage;
InputStream in = urlcon.getInputStream();
while((( c=in.read())!=-1))
{
int all=in.available();
byte[] b =new byte[all];
in.read(b);
webpage =new String(b,"UTF-8");
System.out.println(webpage);
}
in.close();
}
catch(IOException e)
{
System.out.println(""+e);
}
}
catch(MalformedURLException e)
{
System.out.println(""+e);
}
}
}
评论: 0 | 引用: 0 | 查看次数: 239
发表评论