目前负责的项目遇到下载 pdf 文件到本地的需求,有 http 请求和 https 请求发个帖记录一下!
1.http 请求 pdf 文件地址并下载到本地,别的不太会说,直接粘代码了
package cn.com.test; import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import javax.net.ssl.HttpsURLConnection; /** * * @ClassName: HttpDownloaderPDF * @Description: 下载pdf文件到本地 * @author Stone * @date 2016年12月1日 * */ public class HttpDownloaderPDF { String remoteFileUrl = "";//pdf文件请求地址 String localFilePath = "";//本地存放pdf文件路径 public void httpDownloader(String remoteFileUrl, String localFilePath) { try { URL url = new URL(remoteFileUrl); HttpURLConnection httpURLConnection = (HttpsURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(5 * 1000); // 5000 自定义连接超时毫秒数 httpURLConnection.connect(); // 连接 System.out.println("connect URL success!"); int fileLenght = httpURLConnection.getContentLength(); System.out.println("file size:" + (fileLenght / 1024.0) + " KB"); System.out.println("start the download..."); try (DataInputStream dis = new DataInputStream( httpURLConnection.getInputStream()); FileOutputStream fos = new FileOutputStream(localFilePath)) { byte[] buf = new byte[10240]; // 根据实际情况自定义 buf 大小 for (int readSize; (readSize = dis.read(buf)) > 0;) { fos.write(buf, 0, readSize); } System.out.println("download is complete!"); } catch (IOException ex) { System.out.println("download is error"); } httpURLConnection.disconnect(); } catch (IOException ex) { ex.printStackTrace(); System.out.println("URL Does not exist or connection timeout!"); } } } class PDFTest { public static void main(String[] args) { HttpDownloaderPDF hdl = new HttpDownloaderPDF(); hdl.httpDownloader("requestURL", "/home/Stone/Downloader/file.pdf"); } }
2.有 http 请求就应该考虑 https 请求,还是简单粗暴直接粘代码
package cn.com.test; import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; /** * * @ClassName: HttpDownloaderPDF * @Description: 下载pdf文件到本地 * @author Stone * @date 2016年12月1日 * */ public class HttpDownloaderPDF { /** * 验证证书 */ final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; private static void trustAllHosts() { // 创建信任管理器跃过证书校验 TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.X509Certificate[] {}; } public void checkClientTrusted(X509Certificate[] chain, String authType) { } public void checkServerTrusted(X509Certificate[] chain, String authType) { } } }; // 安装 all-trusting 信托管理 try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.printStackTrace(); } } public void httpDownloader(String remoteFileUrl, String localFilePath) { try { trustAllHosts(); URL url = new URL(remoteFileUrl); HttpsURLConnection https = (HttpsURLConnection) url.openConnection(); https.setHostnameVerifier(DO_NOT_VERIFY); HttpURLConnection httpURLConnection = https; httpURLConnection.setConnectTimeout(5 * 1000); // 5000 自定义连接超时毫秒数 httpURLConnection.connect(); // 连接 System.out.println("connect URL success!"); int fileLenght = httpURLConnection.getContentLength(); System.out.println("file size:" + (fileLenght / 1024.0) + " KB"); System.out.println("start the download..."); try (DataInputStream dis = new DataInputStream( httpURLConnection.getInputStream()); FileOutputStream fos = new FileOutputStream(localFilePath)) { byte[] buf = new byte[10240]; // 根据实际情况自定义 buf 大小 for (int readSize; (readSize = dis.read(buf)) > 0;) { fos.write(buf, 0, readSize); } System.out.println("download is complete!"); } catch (IOException ex) { System.out.println("download is error"); } httpURLConnection.disconnect(); } catch (IOException ex) { ex.printStackTrace(); System.out.println("URL Does not exist or connection timeout!"); } } } class PDFTest { public static void main(String[] args) { HttpDownloaderPDF hdl = new HttpDownloaderPDF(); hdl.httpDownloader("requestURL", "/home/Stone/Downloader/file.pdf"); } }
注意:记得在服务器自定义目录下(不要在需要权限的目录下创建,很麻烦还要授权)创建你存文件的文件夹,不然的话程序顺利执行完,但是你要的文件不一定能下载成功!就好比你告诉我,石头哥哥晚上我请你吃饭记得来我家哈!说的挺好听,你不告诉我你家地址和门牌号,我怎么知道去哪里找你,所以啊写程序也得多点儿真诚少点儿套路 0.0
版权声明:本文为本人原创文章,转载请注明出处 java 实现 PDF 文件下载到服务器谢谢!
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于