java 通过文件下载地址读取文件内容
需求:读取文件内容,已知文件下载地址
需要引入pdfbox依赖
<dependency>
<groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.27</version>
</dependency>
@Override
public MesDataRespVO addPasterLabel(PasterLabelReqVO reqVO) {String downloadUrl = "文件下载地址";reqVO.setPaterLabelUrl(downloadUrl);PDDocument document = null;InputStream inputStream = null;try{URL url = new URL(downloadUrl);HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 设置超时时间20秒conn.setConnectTimeout(20 * 1000);inputStream = conn.getInputStream();document = PDDocument.load(inputStream);int pageSize = document.getNumberOfPages();String text = "";// 一页一页读取for (int i = 0; i < pageSize; i++) {// 文本内容PDFTextStripper stripper = new PDFTextStripper();// 设置按顺序输出stripper.setSortByPosition(true);stripper.setStartPage(i + 1);stripper.setEndPage(i + 1);text = stripper.getText(document);System.out.println(text.trim());}}catch (Exception e){log.error(e.getMessage(),e);}finally {try {if (document != null) {document.close();}if(inputStream != null){inputStream.close();}} catch (IOException e) {log.error(e.getMessage(), e);}}return null;
}