在java中如何下载url并将其保存到本地目录中。更重要的是,我想要一个下载的网址(特别是html内容)的离线视图。
发布于 2009-07-03 06:02:53
下面是一些将HTML转换为字符串的代码。注意,这不会拉出内容(图片等),只会拉出HTML!享受:)
try
{
URL url = new URL("http://www.stackoverflow.com");
URLConnection connection = url.openConnection();
connection.setDoInput(true);
InputStream inStream = connection.getInputStream();
BufferedReader input = new BufferedReader(new InputStreamReader(inStream));
String html = "";
String line = "";
while ((line = input.readLine()) != null)
{
html += line;
}
//Now you can do what you please with
//the HTML content (save it locally, parse, etc...)
}
catch(Exception e)
{
//Error handling
}https://stackoverflow.com/questions/1077926
复制相似问题