[java]代码库
package s0220通过url获取资源;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
public class Main {
public static void main(String[] args) throws IOException {
URL url=new URL("http://www.baidu.com");//默认主页
InputStream is=url.openStream();//获取网络流
//获取网络资源,编码格式不同会出现乱码****************
byte[] flush=new byte[1024];
int len=0;
while(-1!=(len=is.read(flush)))
{
System.out.println(new String(flush,0,len));
}
is.close();
//获取网络资源,编码格式不同会出现乱码****************
//解决乱码的方法,转换流
BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream(),"utf-8"));//解码方式,utf-8
String msg=null;
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("百度.html"),"utf-8"));
while((msg=br.readLine())!=null)
{
bw.append(msg);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
}
}