java 程序下载网页

来源:互联网 发布:淘宝管控下架什么意思 编辑:程序博客网 时间:2024/06/02 04:27

参考

http://www.360doc.com/content/08/0708/16/26857_1410575.shtml


package testme;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;public class ReadCsv{public ArrayList<String>urlList=new ArrayList<String>();public ReadCsv(){String file="/home/lyle/Documents/lemurData/test_set_results.csv";try{BufferedReader buf=new BufferedReader(new FileReader(file));String line=null;while((line=buf.readLine())!=null){String []lineArray=line.split(",");String url=new String(lineArray[0].substring(1,lineArray[0].length()-1));urlList.add(url);System.out.println(url+"\t"+lineArray[1]+"\t"+lineArray[2]+"\t"+lineArray.length);}} catch (FileNotFoundException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args){String outFile="/home/lyle/Downloads/23.html";int bufferSize=1024;byte[] b=new byte[bufferSize];try{int readedByte=0;FileOutputStream fo=new FileOutputStream(outFile);URL u=new URL("http://f.xunlei.com/channel");URLConnection uconnection=u.openConnection();BufferedInputStream bf=new BufferedInputStream(uconnection.getInputStream());while((readedByte=bf.read(b))!=-1){fo.write(b,0,readedByte);}fo.close();bf.close();} catch (MalformedURLException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}}}


原创粉丝点击