package httpget; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class demo { private String u; private String encoding; public static void main(String[] args) throws Exception { String url = "http://www.baidu.com/"; demo client = new demo(url, "gb2312"); client.run(); } public demo(String u, String encoding) { this.u = u; this.encoding = encoding; } public void run() throws Exception { File fp = new File("E://zc//test.txt"); URL url = new URL(u); // 根据链接(字符串格式),生成一个URL对象 HttpURLConnection urlConnection = (HttpURLConnection) url .openConnection();// 打开URL BufferedReader reader = new BufferedReader(new InputStreamReader( urlConnection.getInputStream(), encoding)); // 得到输入流,即获得了网页的内容 String line; while ((line = reader.readLine())!= null) { addfile(line,fp); } } public void addfile(String line,File fp) throws IOException //追加文件 { try { String str = "/r/n"; FileOutputStream af = new FileOutputStream(fp,true); af.write(line.getBytes()); af.write(str.getBytes()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("写入失败!"); e.printStackTrace(); } } }
如果不明白上面有些方法的意思可以自己去查API文档
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/17006.html