Java从URL获取PDF内容

Java直接URL获取PDF内容

题外话

网上很多Java通过pdf转 HTML,转文本的,可是通过URL直接获取PDF内容,缺没有,浪费时间,本人最近工作中刚好用到,花了时间整理下,分享出来,防止浪费时间,Apache的pdfbox 2013年都有了。

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;



import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;


/**
 * 通过URL获得PDF内容 转存
 * @author sunyang
 * @date 2019/1/24 14:40
 */
public class PdfText2Url {

    public static void main(String[] args) {
        try {
            //需要获取到的PDF地址
    readPdf("https://img1.xxx.org/tech/file/9bd7/733b/7ef54fbf672cfffaf2b1a6c2.pdf");
        }catch (Exception e){

        }
    }

    /**
     * 传入一个.pdf 地址
     * @param pdfUrl 地址
     * @throws Exception
     */
    public static void readPdf(String pdfUrl) throws Exception {
        // 是否排序
        boolean sort = false;
        // 编码方式
        String encoding = "UTF-8";
        // 开始提取页数
        int startPage = 1;
        // 内存中存储的PDF Document
        PDDocument pdDocument = null;
        //输入流
        InputStream inputStream = null;
        try {
            try {
                // 当作一个URL来装载文件
                URL url = new URL(pdfUrl);
                URLConnection con = url.openConnection();
                con.setConnectTimeout(3 * 1000);
                inputStream = con.getInputStream();
                pdDocument = PDDocument.load(inputStream);
            } catch (MalformedURLException e) {

            }

            // 获取页码
            int endPage = pdDocument.getNumberOfPages();
            PDFTextStripper stripper = null;
            stripper = new PDFTextStripper();
            // 设置是否排序
            stripper.setSortByPosition(sort);
            // 设置起始页
            stripper.setStartPage(startPage);
            // 设置结束页
            stripper.setEndPage(endPage);
            System.out.println(stripper.getText(pdDocument));
            System.out.println(" 输出成功!");
        } finally {
            if (inputStream != null) {
                // 关闭输出流
                inputStream.close();
            }
            if (pdDocument != null) {
                // 关闭PDF Document
                pdDocument.close();
            }
        }
    }

}

Pom.xml 添加Apache的pdfbox,已经很成熟了

<dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>fontbox</artifactId>
      <version>2.0.7</version>
    </dependency>

    <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox</artifactId>
      <version>2.0.4</version>
    </dependency>

转载于:https://www.cnblogs.com/sunyk/p/10337747.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取其他类型的文件流类似,我们可以使用 Apache HttpComponents 库中的 HttpPost 类来发送 POST 请求并获取 PDF 文件流。以下是示例代码: ``` import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; public class HttpPostExample { public static void main(String[] args) throws Exception { String url = "http://example.com/pdf"; String filePath = "/path/to/file.pdf"; // Create POST request HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(url); // Add file to request body HttpEntity entity = MultipartEntityBuilder.create() .addBinaryBody("file", new File(filePath)) .build(); post.setEntity(entity); // Send request and receive response HttpResponse response = client.execute(post); HttpEntity responseEntity = response.getEntity(); // Get PDF file stream from response entity InputStream inputStream = responseEntity.getContent(); // Use PDF file stream as needed // ... // Release resources EntityUtils.consume(responseEntity); } } ``` 在上面的代码中,我们首先创建了一个 HttpClient 对象,然后创建一个 HttpPost 对象并设置 URL。接下来,我们使用 MultipartEntityBuilder 类创建一个包含 PDF 文件的请求体,并将其设置为 HttpPost 对象的实体。最后,我们发送请求并从响应中获取 PDF 文件流。注意,我们需要手动释放响应实体的资源,以避免内存泄漏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值