import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
public class Tess {
public static void main(String[] args) {
//使用URL类的openStream()成员方法获取URL指定的网上信息
String str = "https://www.xxx.com/xs/250/250006/";//目标资源的部分url
try {
//URL对象可以访问已知URL的网上资源
URL url = null;
int begin = 504457;
for (int i = 0; i < 62; i++) {
int num = begin + i;
String targetURL = str + num + ".html";//网上资源的完整url
url = new URL(targetURL);//形参是网上资源的网址
//用openStream()会在网上资源服务器和Java客户端之间建立输入字节流对象
InputStream inputStream = url.openStream();
//为了IO效率高,用缓冲字符流把网上资源读入内存
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//把jvm内存数据输出到磁盘中指定位置
String locate="C:\\Users\\use\\Desktop\\novel\\"+(i+1)+".html";
BufferedWriter out = new BufferedWriter(new FileWriter(locate));
String context;
while ((context = bufferedReader.readLine()) != null) {//判断输入流是否读到文件末尾
//把网上信息读入jvm内存中
//把内存内容输出到文件中
out.write(context);
out.newLine();//向文件中写入一个回车符号
}
inputStream.close();
bufferedReader.close();
out.close();
}
} catch (MalformedURLException e) {
System.out.println("Can't get URL:" + e.getMessage());
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
用java下载html页面
最新推荐文章于 2023-03-31 10:41:10 发布