HtmlSaveUtil.java
import java.io.*;
import java.net.URL;
public class HtmlSaveUtil {
public static void saveHtml(String filepath, String str) throws IOException {
//字节输出流
FileOutputStream fos = new FileOutputStream(new File(filepath));
//接收字节输入流
InputStream is = new URL(str).openStream();
//为字节输入流加缓冲
BufferedInputStream bis = new BufferedInputStream(is);
//为字节输出流加缓冲
BufferedOutputStream bos = new BufferedOutputStream(fos);
int length;
byte[] bytes = new byte[1024*20];
while((length = bis.read(bytes, 0, bytes.length)) != -1){
fos.write(bytes, 0, length);
}
bos.close();
fos.close();
bis.close();
is.close();
}
public static void main(String[] args) throws IOException {
String url="https://blog.csdn.net/qq_35387940/article/details/129611551";
HtmlSaveUtil.saveHtml("D:\\blog\\129611551.html",url);
}
}
运行main方法, 效果:
看一眼,可以看到整个网页的代码:
好了,就到这。