java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java url下载pdf

java实现从url路径中下载pdf文档到本地

作者:小二爱编程·

这篇文章主要为大家详细介绍了java实现从url路径中下载pdf文档到本地,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下

这篇文章主要为大家介绍了如何从指定的网络URL下载PDF文件。主要通过HttpURLConnection建立连接,设置超时和User-Agent,读取输入流并将其写入本地文件系统。以下是完整代码:

package com.cellstrain.icell.util;


import java.io.*;
import java.net.*;

public class DownloadPdf {

    /**
     * 从网络Url中下载文件
     * @param urlStr
     * @param fileName
     * @param savePath
     * @throws IOException
     */
    public static void  downLoadByUrl(String urlStr,String fileName,String savePath) throws IOException{
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        //设置超时间为3秒
        conn.setConnectTimeout(5*1000);
        //防止屏蔽程序抓取而返回403错误
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        //得到输入流
        InputStream inputStream = conn.getInputStream();
        //获取自己数组
        byte[] getData = readInputStream(inputStream);
        //文件保存位置
        File saveDir = new File(savePath);
        if(!saveDir.exists()){
            saveDir.mkdir();
        }
        File file = new File(saveDir+File.separator+fileName);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(getData);
        if(fos!=null){
            fos.close();
        }
        if(inputStream!=null){
            inputStream.close();
        }
        System.out.println("info:"+url+" download success");

    }


    /**
     * 从输入流中获取字节数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }

    public static void main(String[] args) {
        try{
            downLoadByUrl("https://www.mybiosource.com/images/tds/protocol_samples/MBS700_Antibody_Set_Sandwich_ELISA_Protocol.pdf",
                    "ELISA.pdf","E:/upload/protocol");
        }catch (Exception e) {
            // TODO: handle exception
        }
    }
}

方法补充

1.java实现从url路径中下载pdf文档到本地

在开始之前,确保你的计算机上安装了Java开发环境(JDK)。建议使用Java 8及以上版本。你可以使用任何开发工具,例如Eclipse、IntelliJ IDEA或简单的文本编辑器。

下载PDF文档的步骤

下载PDF文档的步骤如下:

代码示例

下面是一个简单的Java代码示例,演示了如何从URL路径下载PDF文档到本地。

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class PdfDownloader {
    
    public static void downloadPdf(String pdfUrl, String destinationFile) {
        try {
            URL url = new URL(pdfUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setDoOutput(true);
            
            int responseCode = httpURLConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) { // 200 status code
                InputStream inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
                FileOutputStream fileOutputStream = new FileOutputStream(destinationFile);
                
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, bytesRead);
                }
                
                fileOutputStream.close();
                inputStream.close();
                System.out.println("PDF downloaded successfully: " + destinationFile);
            } else {
                System.out.println("Failed to download PDF, response code: " + responseCode);
            }
            httpURLConnection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        String pdfUrl = " // 替换为实际PDF链接
        String destinationFile = "downloaded.pdf"; // 目标文件名
        downloadPdf(pdfUrl, destinationFile);
    }
}

代码详解

2.Java根据URL下载文件到本地的2种方式(大型文件与小型文件)

a.小型文件推荐使用

代码解析

实现代码

public static void downloadFile(String remoteFilePath, String localFilePath) {
        URL website = null;
        ReadableByteChannel rbc = null;
        FileOutputStream fos = null;
        try {
            website = new URL(remoteFilePath);
            rbc = Channels.newChannel(website.openStream());
            fos = new FileOutputStream(localFilePath);//本地要存储的文件地址 例如:test.txt
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(rbc!=null){
                try {
                    rbc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
 
            }
        }
    }

b.大型文件推荐使用

代码解析

实现代码

public static void downloadFile1(String downloadUrl, String path){
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            URL url = new URL(downloadUrl);
            //这里没有使用 封装后的ResponseEntity 就是也是因为这里不适合一次性的拿到结果,放不下content,会造成内存溢出
            HttpURLConnection connection =(HttpURLConnection) url.openConnection();
 
            //使用bufferedInputStream 缓存流的方式来获取下载文件,不然大文件会出现内存溢出的情况
            inputStream = new BufferedInputStream(connection.getInputStream());
            File file = new File(path);
            if (file.exists()) {
                file.delete();
            }
            outputStream = new FileOutputStream(file);
            //这里也很关键每次读取的大小为5M 不一次性读取完
            byte[] buffer = new byte[1024 * 1024 * 5];// 5MB
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            connection.disconnect();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            IOUtils.closeQuietly(outputStream);
            IOUtils.closeQuietly(inputStream);
        }
    }

到此这篇关于java实现从url路径中下载pdf文档到本地的文章就介绍到这了,更多相关java url下载pdf内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文