Java通过URL类下载图片的实例代码

2023-05-16 0 3,959

目录

Java通过URL类下载图片

一、概述

URL(Uniform Resource Locator) :统一资源定位符,它表示 Internet 上 某一 资源 的地址。 它是一种具体的 URI ,即 URL 可以用来标识一个资源,而且还指明了如何 locate 这个资源。 通过 URL 我们可以访问 Internet 上的各种网络资源,比如最常见的 www , ftp 站点。浏览器通过解析给定的 URL 可以在网络上查找相应的文件或其他资源。 URL 的基本结构由 5 部分组成: < 传输协议 >://< 主机名 >:< 端口号 >/< 文件名 ># 片段名 ? 参数列表

二、通过URL下载图片

HttpsURLConnection httpsURLConnection = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            //1.创建URL对象
            URL url = new URL(\"https://img1.baidu.com/it/u=3009731526,373851691&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500\");
            //2.与URL建立连接:首先要在一个 URL 对象上通过方法 openConnection() 生成对应的 URLConnection
            //对象。
            httpsURLConnection = (HttpsURLConnection) url.openConnection();
            httpsURLConnection.connect();
            //3.获取输入流,并创建输出流对象
            is = httpsURLConnection.getInputStream();
            fos = new FileOutputStream(new File(\"test.jpg\"));
            //4.输出图片
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.关闭资源
            try {
                if (is != null)
                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (httpsURLConnection != null)
                httpsURLConnection.disconnect();
        }

扩展:java通过url获取图片文件

1. 根据url下载Url中的图片

import java.net.URL;
import java.io.InputStream;
import java.io.FileOutputStream;

public class ImageDownloader {
    public static void main(String[] args) throws Exception {
        // URL of the image to download
        String imageUrl = \"https://example.com/image.jpg\";
        
        // Create URL object and open input stream to the image
        URL url = new URL(imageUrl);
        InputStream inputStream = url.openStream();
        
        // Output stream to save the image to file
        FileOutputStream outputStream = new FileOutputStream(\"image.jpg\");
        
        // Read bytes from the input stream and write to the output stream
        byte[] buffer = new byte[2048];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
        
        // Close streams
        inputStream.close();
        outputStream.close();
        
        System.out.println(\"Image downloaded successfully.\");
    }
}

2. 根据get请求url下载Url中的图片

import java.net.URL;
import java.io.InputStream;
import java.io.FileOutputStream;

public class ImageDownloader {
    public static void main(String[] args) throws Exception {
        // URL of the image to download
        String imageUrl = \"https://example.com/image.jpg\";
        
        // Create URL object and open input stream to the image
        URL url = new URL(imageUrl);
        InputStream inputStream = url.openStream();
        
        // Output stream to save the image to file
        FileOutputStream outputStream = new FileOutputStream(\"image.jpg\");
        
        // Read bytes from the input stream and write to the output stream
        byte[] buffer = new byte[2048];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
        
        // Close streams
        inputStream.close();
        outputStream.close();
        
        System.out.println(\"Image downloaded successfully.\");
    }
}

3. 考虑url中携带中文,需要做转义

    imageUrl = URLEncoder.encode(imageUrl, \"utf-8\")
            .replaceAll(\"%3A\", \":\")
            .replaceAll(\"%2F\", \"/\")
            .replaceAll(\"%2C\", \",\")
            .replaceAll(\"%7B\", \"{\")
            .replaceAll(\"%3F\",\"?\")
            .replaceAll(\"%7D\", \"}\")
            .replaceAll(\"%26\",\"&\")
            .replaceAll(\"%3D\",\"=\");
    //new一个URL对象
    URL url = new URL(imageUrl);
资源下载此资源下载价格为1小猪币,终身VIP免费,请先
由于本站资源来源于互联网,以研究交流为目的,所有仅供大家参考、学习,不存在任何商业目的与商业用途,如资源存在BUG以及其他任何问题,请自行解决,本站不提供技术服务! 由于资源为虚拟可复制性,下载后不予退积分和退款,谢谢您的支持!如遇到失效或错误的下载链接请联系客服QQ:442469558

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 Java通过URL类下载图片的实例代码 https://www.20zxx.cn/704785/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务