从URL读取资源文件:
public static void main(String [] args) throws Exception {
String urlString = "http://xxx/xxx.html";
System.out.println(getDocumentAt(urlString));
}
public static String getDocumentAt(String urlString) {
//此方法兼容HTTP和FTP协议
StringBuffer document = new StringBuffer();
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ( (line = reader.readLine()) != null) {
document.append(line + " ");
}
reader.close();
} catch (MalformedURLException e) {
System.out.println("Unable to connect to URL: " + urlString);
} catch (IOException e) {
System.out.println("IOException when connecting to URL: " + urlString);
}
return document.toString();
}