IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> 下载远程文件到本地 -> 正文阅读

[网络协议]下载远程文件到本地

1、忽略HTTPS请求的SSL证书

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 {

    public 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) {
                return true;
            }
        };
        trustAllHttpsCertificates();
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
    }
}

2、下载文件util:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpURLConnectionUtil {

    private static Logger logger = LoggerFactory.getLogger(HttpURLConnectionUtil.class);

    /**
     * 通过url下载文件到指定文件夹
     * @param downloadUrl	url
     * @param downloadDir	目标目录
     */
    public static void downloadFile(String downloadDir,String downloadUrl) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            try {
                SslUtils.ignoreSsl();
            } catch (Exception e) {
                e.printStackTrace();
            }
            String docname = downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1);
            downloadUrl = downloadUrl.substring(0,downloadUrl .lastIndexOf("/"))+"/"+URLEncoder.encode(docname,"utf-8");
            //获取连接
            URL url=new URL(downloadUrl);
            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
            connection.setConnectTimeout(4*1000) ;
            connection.setRequestMethod("GET") ;
            connection.setRequestProperty(
                    "Accept",
                    "image/gif, image/jpeg, image/pjpeg, image/pjpeg, " +
                            "application/x-shockwave-flash, application/xaml+xml, " +
                            "application/vnd.ms-xpsdocument, application/x-ms-xbap, " +
                            "application/x-ms-application, application/vnd.ms-excel, " +
                            "application/vnd.ms-powerpoint, application/msword, */*");
            connection.setRequestProperty("Accept-Language", "zh-CN");
            connection.setRequestProperty("Charset", "UTF-8");
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            //设置浏览器类型和版本、操作系统,使用语言等信息
            connection.setRequestProperty(
                    "User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; " +
                            ".NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; " +
                            ".NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
            //设置为长连接
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Accept", "*/*");
            //获取输入流
            inputStream=connection.getInputStream();
            File fileDir=new File(downloadDir);
            if(!fileDir.exists()){//如果文件夹不存在
                fileDir.mkdirs();//创建文件夹
            }

            //截取文件名称,可以把 / 换成需要的规则
            String filePath = downloadDir+File.separator + docname;
            File file = new File(filePath);
            file.createNewFile();//创建文件,存在覆盖

            outputStream = new FileOutputStream(file);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = inputStream.read(buf, 0, 1024)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.flush();
            connection.disconnect();
        } catch (Exception e) {
            logger.error("文件下载出错:" + e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                logger.error("关闭流出错:" + e);
            }
        }
    }

    /**
     * 压缩并下载压缩包
     */
    /*public static void downloadZip(HttpServletRequest request, HttpServletResponse res,
                            String downloadDir,
                            String downloadZip, String zipName) throws IOException {
        OutputStream out = null;
        File zip = null;
        try{
            //多个文件进行压缩,批量打包下载文件
            //创建压缩文件需要的空的zip包

            String zipFilePath = downloadZip+File.separator+zipName;
            File fileDir=new File(downloadZip);
            if(!fileDir.exists()){//如果文件夹不存在
                fileDir.mkdir();//创建文件夹
            }

            //压缩文件
            zip = new File(zipFilePath);
            zip.createNewFile();//创建文件,存在覆盖

            //创建zip文件输出流
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
            zipFile(downloadDir, zos);
            zos.close();

            //将打包后的文件写到客户端,输出的方法同上,使用缓冲流输出
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(zipFilePath));
            byte[] buff = new byte[bis.available()];
            bis.read(buff);
            bis.close();

            //IO流实现下载的功能
            res.setCharacterEncoding("UTF-8"); //设置编码字符
            res.setContentType("application/octet-stream;charset=UTF-8"); //设置下载内容类型application/octet-stream(二进制流,未知文件类型);

            //防止文件名乱码
            String userAgent = request.getHeader("USER-AGENT");
            if (userAgent.contains("Firefox") || userAgent.contains("firefox")) {//火狐浏览器
                zipName = new String(zipName.getBytes(), "ISO8859-1");
            } else {
                zipName = URLEncoder.encode(zipName, "UTF-8");//其他浏览器
            }

            res.setHeader("Content-disposition", "attachment;filename="+zipName);//设置下载的压缩文件名称
            out = res.getOutputStream();   //创建页面返回方式为输出流,会自动弹出下载框
            out.write(buff);//输出数据文件
        }catch(Exception e) {
            logger.error("压缩包下载出错:" + e);
        }finally {
            if(out != null){
                out.flush();//释放缓存
                out.close();//关闭输出流
            }

            //下载完后删除文件夹和压缩包
            File fileDir = new File(downloadDir);
            FileUtils.deleteDirectory(fileDir);

            if(zip != null){
                zip.delete();
            }
        }
    }*/

    /**
     * 压缩文件
     * @param filePath	需要压缩的文件夹
     * @param zos	zip文件输出流
     */
   /* private static void zipFile(String filePath,ZipOutputStream zos) throws IOException {
        File inputFile = new File(filePath);  //根据文件路径创建文件
        if(inputFile.exists()) { //判断文件是否存在
            if (inputFile.isFile()) {  //判断是否属于文件,还是文件夹
                //创建输入流读取文件
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile));

                //将文件写入zip内,即将文件进行打包
                zos.putNextEntry(new ZipEntry(inputFile.getName()));

                //写入文件的方法,同上
                int size = 0;
                byte[] buffer = new byte[1024];  //设置读取数据缓存大小
                while ((size = bis.read(buffer)) > 0) {
                    zos.write(buffer, 0, size);
                }
                //关闭输入输出流
                zos.closeEntry();
                bis.close();

            } else {  //如果是文件夹,则使用穷举的方法获取文件,写入zip
                try {
                    File[] files = inputFile.listFiles();
                    for (File fileTem:files) {
                        zipFile(fileTem.toString(),zos);
                    }
                } catch (Exception e) {
                    logger.error("压缩文件出错:" + e);
                }
            }
        }
    }*/
}

说明:

1)SslUtils.ignoreSsl();防止https请求失败,解决javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException报错

2)downloadUrl = downloadUrl.substring(0,downloadUrl .lastIndexOf("/"))+"/"+URLEncoder.encode(docname,"utf-8");

对文件名进行编码,因为HttpURLConnection无法打开含有中文的链接,解决文件路径404报错。

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-03-08 22:56:03  更:2022-03-08 22:59:30 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/5 13:19:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码