根据网络地址下载文件到本地


    public static void test() {
        String fileName = "D:\\222.png";
        try {
            HttpClientUtil.httpDownload("http://10.50.13.99:9017/image/202303291048_6b74e443-9d54-4b8e-a7cc-843a72a2608a",fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 功能描述: 下载网络文件
     *
     * @param httpUrl
     * @param dest
     * @return boolean
     * @author 
     * @version 1.0
     */
    public static boolean httpDownload(String httpUrl, String dest) throws IOException {
        // 1.下载网络文件
        int byteRead;
        URL url;
        try {
            url = new URL(httpUrl);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
            return false;
        }
        URLConnection conn = null;
        InputStream inStream = null;
        FileOutputStream fs = null;
        try {
            //2.获取链接
            conn = url.openConnection();
            //3.输入流
            inStream = conn.getInputStream();
            //3.写入文件
            fs = new FileOutputStream(dest);

            byte[] buffer = new byte[1024];
            while ((byteRead = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteRead);
            }
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            try {
                if (fs != null){
                    fs.close();
                }
                if (inStream != null){
                    inStream.close();
                }
            } catch (IOException ex) {
                logger.error(ex.getMessage());
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值