Network resources to download

1, the input stream through java pdf obtain processed packet network (the java.net) correlation method;

2, acquires an array of bytes from the input stream;

3, the output stream into the specified folder;

4, close the input stream acquired network;

 

The so-called 工欲善其事必先利其器, we first need to prepare:

SslUtils tools: the tools is very important, mainly to allow the server to ignore the trust certificate issue, if you do not use this tool class occurs server does not trust the certificate we created yourself, throw SSLHandshakeException;

package com.sunfreeter;

import java.security.cert.CertificateException;
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;

public class SslUtils {
    private static void trustAllHttpsCertificates() throws Exception {
        TrustManager[] trustAllCerts = new TrustManager[1];
        TrustManager tm = new miTM();
        trustAllCerts[0] = tm;
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    }

    static class miTM implements TrustManager, X509TrustManager {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public boolean isServerTrusted(X509Certificate[] certs) {
            return true;
        }

        public boolean isClientTrusted(X509Certificate[] certs) {
            return true;
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
            return;
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
            return;
        }
    }

    /**
     * 忽略HTTPS请求的SSL证书,必须在openConnection之前调用
     * 
     * @throws Exception
     */
    public static void ignoreSsl() throws Exception {
        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
                return true;
            }
        };
        trustAllHttpsCertificates();
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
    }
}

IoCommonUtil: mainly reading and writing a stream file

package com.sunfreeter.thread;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class IoCommonUtil {
    /**
     * 从输入流中获取字节数组
     * @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();
    }

 /**
  * 写入文件
  * @param savePath
  * @param fileName
  * @param getData
  * @throws IOException
  */
    public static void writeOutputStream(String savePath,String fileName,byte[] getData) throws IOException {
         //文件保存位置
        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();
        }
    }
}

With the tools, we can begin ~~

Step 1: Get pdf java input stream by the packet processing network (the java.net) correlation method;

URL url = new URL(urlStr);
        SslUtils.ignoreSsl();
        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();

Step 2: Get byte array from the input stream;

 //获取字节数组
  byte[] getData = IoCommonUtil.readInputStream(inputStream);

Step 3: the output stream into the specified folder;

 IoCommonUtil.writeOutputStream(savePath, fileName, getData);

Step 4: Get input stream off the network;

 if(inputStream!=null){
            inputStream.close();
  }

Finally, attach a complete code + the above four steps to test, you only need to copy and paste (Please call me Lei Feng patriarch, left):

package com.sunfreeter.thread;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class FileManagerSystem {

    public static void downLoadByUrl(String urlStr, String fileName, String savePath) throws Exception {
        URL url = new URL(urlStr);
        SslUtils.ignoreSsl();
        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 = IoCommonUtil.readInputStream(inputStream);
        IoCommonUtil.writeOutputStream(savePath, fileName, getData);
        if (inputStream != null) {
            inputStream.close();
        }
        System.out.println("info:" + url + " download success");
    }

    public static void main(String[] args) throws Exception {
        String fileUrl = "https://www.debian.org/releases/stable/amd64/install.pdf.zh-cn";
        FileManagerSystem.downLoadByUrl(fileUrl, "test.pdf", "F:\\迅雷下载");
    }
}

 

Guess you like

Origin www.cnblogs.com/zhengjinsheng/p/11114574.html