Java——获取网页内容并在本地生成HTML文件

使用java.net包下的URL类,可以将一个网页(链接)封装成一个URL对象。
URL对象有一个openStream()方法,使用该方法可以获取该网页的输入流,我们可以通过读取输入流的方式获得网页的内容,并通过输出流写入HTML文件中。


方式一:

使用此方法需要指定输入流和输出流的字符编码,否则可能会出现乱码

步骤:
1. 通过URL对象的openStream()方法获得网页的字节输入流
2. 将字节输入流转换成字符输入流
3. 为字符输入流加缓冲区
4. 定义一个字节输出流
5. 将字节输出流转换成字符输出流
6. 将读取到的数据写入文件
7. 关闭流

public void way_1() throws Exception {
    InputStream inputStream;//接收字节输入流
    InputStreamReader inputStreamReader;//将字节输入流转换成字符输入流
    BufferedReader bufferedReader;//为字符输入流加缓冲
    FileOutputStream fileOutputStream;//字节输出流
    OutputStreamWriter outputStreamWriter;//将字节输出流转换成字符输出流

    URL wangyi = new URL("http://www.163.com/");
    inputStream = wangyi.openStream();
    inputStreamReader = new InputStreamReader(inputStream, "gb2312");
    bufferedReader = new BufferedReader(inputStreamReader);
    String s;
    File dest = new File("src/wangyi.html");
    fileOutputStream = new FileOutputStream(dest);
    outputStreamWriter = new OutputStreamWriter(fileOutputStream, "gb2312");
    while ((s = bufferedReader.readLine()) != null) {
        outputStreamWriter.write(s);
    }

    outputStreamWriter.close();
    fileOutputStream.close();
    bufferedReader.close();
    inputStreamReader.close();
    inputStream.close();
}

方式二:

使用此方法不需要指定输入流和输出流的字符编码(因为是通过字节的方式)

步骤:
1. 通过URL对象的openStream()方法获得网页的字节输入流
2. 为字节输入流加缓冲
3. 创建字节输出流对象
4. 为字节输出流加缓冲
5. 读取数据,并写入HTML文件
6. 关闭流

public void way_2() throws Exception{
    File dest = new File("src/wangyi2.html");
    InputStream is;//接收字节输入流
    FileOutputStream fos = new FileOutputStream(dest);//字节输出流

    URL wangyi = new URL("http://www.163.com/");
    is = wangyi.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();
}
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
你可以通过以下步骤实现: 1. 在后台接收到pdf二进制流文件后,将其保存为byte[]数组。 2. 将byte[]数组换为InputStream对象。 3. 使用PdfBox库(或其他pdf处理库)将InputStream对象换为PDF文档对象(PDDocument)。 4. 根据需要,对PDF文档进行修改(例如添加水印、修改内容等)。 5. 将修改后的PDF文档保存到本地文件系统。 下面是一个简单的Java代码示例,演示如何将接收到的pdf二进制流文件保存到本地: ```java import org.apache.pdfbox.pdmodel.PDDocument; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class PdfFileHandler { public void savePdf(byte[] pdfBytes, String savePath) throws IOException { // 将byte[]数组换为InputStream对象 InputStream inputStream = new ByteArrayInputStream(pdfBytes); // 使用PdfBox库将InputStream对象换为PDDocument对象 PDDocument document = PDDocument.load(inputStream); // 将PDDocument对象保存为本地文件 document.save(new File(savePath)); // 关闭PDDocument对象 document.close(); } public static void main(String[] args) throws IOException { byte[] pdfBytes = // 从请求中获取pdf二进制流文件 String savePath = "C:/temp/test.pdf"; // 保存路径 PdfFileHandler handler = new PdfFileHandler(); handler.savePdf(pdfBytes, savePath); } } ``` 请注意,本示例仅演示了最基本的保存操作。在实际使用中,您可能需要根据具体的需求进行修改和扩展。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值