iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java通过URL类下载图片的实例代码
  • 744
分享到

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

Python 官方文档:入门教程 => 点击学习

摘要

目录Java通过URL类下载图片一、概述二、通过URL下载图片扩展:java通过url获取图片文件1. 根据url下载Url中的图片2. 根据get请求url下载Url中的图片3.

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("/file/imgs/upload/202302/22/23vwdkbnjob.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类下载图片的文章就介绍到这了,更多相关java通过URL类下载图片内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java通过URL类下载图片的实例代码

本文链接: https://www.lsjlt.com/news/197168.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作