通过接口获取的html文件进行解析

package com.cjl.industrial.application.bottle.controller;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;

public class test {


    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("D:\\123.docx");
        // 这里是你要写入的文件
        XWPFDocument xdoc = new XWPFDocument(fileInputStream);
        // 创建一个段落
        XWPFParagraph xpara = xdoc.createParagraph();
        // 一个XWPFRun代表具有相同属性的一个区域。
        XWPFRun run = xpara.createRun();
        // 获取连接客户端工具
            CloseableHttpClient httpClient = HttpClients.createDefault();
            String entityStr = null;
            CloseableHttpResponse response = null;
            try {
                // 创建POST请求对象
                for(int b=1;b<=25;b++){
                    String url ="http://www.renyiwang.net/Student/PracticePreview.asp?o_id=6&q_id=2501&questions=&pn="+b;
                    HttpPost httpPost = new HttpPost(url);
                    //登录信息
                    //httpPost.addHeader("Cookie", "");
                    response = httpClient.execute(httpPost);
                    // 获得响应的实体对象
                    HttpEntity entity = response.getEntity();
                    // 使用Apache提供的工具类进行转换成字符串
                    entityStr = EntityUtils.toString(entity, "UTF-8");
                    org.jsoup.nodes.Document document = Jsoup.parse(entityStr);
                    Elements tr = document.getElementsByTag("tr");
                    for(int a=0;a<tr.size();a++){
                        Element element = tr.get(a);
                        String text = element.text();
                        run.setText(text+"\r");
                        run = xpara.createRun();
                    }
                }
                OutputStream os = new FileOutputStream("D://345.docx");
                xdoc.write(os);
                os.close();

            } catch (ClientProtocolException e) {
                System.err.println("Http协议出现问题");
                e.printStackTrace();
            } catch (ParseException e) {
                System.err.println("解析错误");
                e.printStackTrace();
            } catch (IOException e) {
                System.err.println("IO异常");
                e.printStackTrace();
            } finally {
                // 释放连接
                if (null != response) {
                    try {
                        response.close();
                        httpClient.close();
                    } catch (IOException e) {
                        System.err.println("释放连接出错");
                        e.printStackTrace();
                    }
                }
            }

            // 打印响应内容
            System.out.println(entityStr);
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,我们可以使用URL类和URLConnection类来根据URL接口获取文件名称和类型。 首先,我们需要创建一个URL对象,将URL字符串作为参数传递给它的构造函数。然后,我们可以通过调用URL对象的openConnection()方法创建一个URLConnection对象。 接下来,我们可以调用URLConnection对象的getContentType()方法获取文件的类型。这个方法会返回一个表示文件类型的字符串,比如"text/html"表示HTML文件。我们可以根据需要进行进一步的处理和判断。 另外,我们也可以通过调用URLConnection对象的getHeaderFields()方法获取响应头中的字段信息。其中,文件名称通常存储在Content-Disposition字段中。我们可以通过对该字段进行解析和处理,获取文件的原始名称。 下面是一段示例代码,演示了如何根据URL接口获取文件名称和类型的过程: ``` import java.io.IOException; import java.net.URL; import java.net.URLConnection; public class FileNameAndType { public static void main(String[] args) { String urlString = "https://example.com/example.pdf"; try { URL url = new URL(urlString); URLConnection connection = url.openConnection(); String contentType = connection.getContentType(); System.out.println("文件类型: " + contentType); String contentDisposition = connection.getHeaderField("Content-Disposition"); if (contentDisposition != null && contentDisposition.contains("filename=")) { String fileName = contentDisposition .substring(contentDisposition.indexOf("filename=") + 9) .replace("\"", ""); System.out.println("文件名称: " + fileName); } } catch (IOException e) { e.printStackTrace(); } } } ``` 请注意,在使用这段代码时,我们需要将"urlString"变量替换为实际的URL接口。同时,也需要处理可能出现的异常情况,比如网络连接失败等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值