温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java怎么通过URL类下载图片

发布时间:2023-02-22 13:54:33 来源:亿速云 阅读:118 作者:iii 栏目:开发技术

本篇内容介绍了“Java怎么通过URL类下载图片”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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://cache.yisu.com/upload/information/20230222/112/3816.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);

“Java怎么通过URL类下载图片”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI