文件下载功能

本文介绍了一个用于从HTTPS源下载文件的Java工具类。该工具通过指定URL路径和下载目录来实现文件的下载,并包含了错误处理机制确保下载过程的稳定性和安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpsConnectionUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(HttpsConnectionUtil.class);

    private HttpsConnectionUtil() {

    }

    public static File downloadFile(String urlPath, String downloadDir) {
        HttpURLConnection httpURLConnection = null;
        try {
            URL url = new URL(urlPath);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.connect();
        } catch (MalformedURLException e) {
            LOGGER.error("Get MalformedURLException.", e);
        } catch (IOException e) {
            LOGGER.error("Get IOException.", e);
        }

        File file = null;
        if (null == httpURLConnection) {
            return file;
        }

        int responseCode = 404;
        try {
            responseCode = httpURLConnection.getResponseCode();
        } catch (IOException e) {
            LOGGER.error("Get response code error.", e);
            return file;
        }

        if (responseCode == HttpURLConnection.HTTP_OK) {
            OutputStream out = null;
            try (BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream())) {
                // 文件名
                String filePathUrl = httpURLConnection.getURL().getFile();
                String fileFullName = filePathUrl.substring(filePathUrl.lastIndexOf('/') + 1);

                String path = downloadDir + '/' + fileFullName;
                file = new File(path);
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }

                int size = 0;
                byte[] buff = new byte[1024];

                out = new FileOutputStream(file);
                while ((size = bin.read(buff)) != -1) {
                    out.write(buff, 0, size);
                }
            } catch (IOException e) {
                LOGGER.info("Download file error.", e);
            } finally {
                try {
                    if (null != out) {
                        out.close();
                    }
                } catch (IOException e) {
                    LOGGER.error("Close I/O error.", e);
                }
            }
        } else {
            LOGGER.info("Download file: {} failed, responseCode:{}.", urlPath, responseCode);
        }
        return file;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值