日期:2014-05-20 浏览次数:20721 次
public static String getPageContent(String url){ String pageContent = ""; try { File file = new File(url); BufferedReader in = new BufferedReader(new FileReader(file)); String str = in.readLine(); while (str != null) { pageContent += str + "\n"; str = in.readLine(); if (null == str) { break; } } } catch (Exception e) { e.printStackTrace(); } return pageContent; }
------解决方案--------------------
以前写过一个类似的 帮你修改了下
import java.io.InputStream; import java.net.URL; import java.net.URLConnection; public class URLconnectionTest { public static void main(String[] args) throws Exception { String content = getPageContent("http://www.baidu.com"); System.out.println(content); } public static String getPageContent(String url) throws Exception { String pageContent = null; URL url2 = new URL(url); URLConnection urlcon = url2.openConnection(); System.out.println(urlcon.getHeaderField("Content-Type")); InputStream is = urlcon.getInputStream(); StringBuffer sbf = new StringBuffer(); byte[] buffer = new byte[2048]; int length = 0; while(-1 != (length = is.read(buffer, 0, buffer.length))) { sbf.append(new String(buffer,0,length)); } pageContent = sbf.toString(); return pageContent; } }
------解决方案--------------------
我觉得LZ你应该把这3个问题分别用google查一下~!
------解决方案--------------------