欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java通過URL類下載圖片的實(shí)例代碼

 更新時(shí)間:2023年02月22日 11:58:19   作者:星空彼岸007  
這篇文章主要介紹了Java通過URL類下載圖片,文中結(jié)合實(shí)例代碼補(bǔ)充介紹了java通過url獲取圖片文件的相關(guān)知識,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Java通過URL類下載圖片

一、概述

URL(Uniform Resource Locator) :統(tǒng)一資源定位符,它表示 Internet 上 某一 資源 的地址。 它是一種具體的 URI ,即 URL 可以用來標(biāo)識一個(gè)資源,而且還指明了如何 locate 這個(gè)資源。 通過 URL 我們可以訪問 Internet 上的各種網(wǎng)絡(luò)資源,比如最常見的 www , ftp 站點(diǎn)。瀏覽器通過解析給定的 URL 可以在網(wǎng)絡(luò)上查找相應(yīng)的文件或其他資源。 URL 的基本結(jié)構(gòu)由 5 部分組成: < 傳輸協(xié)議 >://< 主機(jī)名 >:< 端口號 >/< 文件名 ># 片段名 ? 參數(shù)列表

二、通過URL下載圖片

HttpsURLConnection httpsURLConnection = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            //1.創(chuàng)建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建立連接:首先要在一個(gè) URL 對象上通過方法 openConnection() 生成對應(yīng)的 URLConnection
            //對象。
            httpsURLConnection = (HttpsURLConnection) url.openConnection();
            httpsURLConnection.connect();
            //3.獲取輸入流,并創(chuàng)建輸出流對象
            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.關(guān)閉資源
            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();
        }

擴(kuò)展:java通過url獲取圖片文件

1. 根據(jù)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. 根據(jù)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中攜帶中文,需要做轉(zhuǎn)義

    imageUrl = URLEncoder.encode(imageUrl, "utf-8")
            .replaceAll("%3A", ":")
            .replaceAll("%2F", "/")
            .replaceAll("%2C", ",")
            .replaceAll("%7B", "{")
            .replaceAll("%3F","?")
            .replaceAll("%7D", "}")
            .replaceAll("%26","&")
            .replaceAll("%3D","=");
    //new一個(gè)URL對象
    URL url = new URL(imageUrl);

到此這篇關(guān)于Java通過URL類下載圖片的文章就介紹到這了,更多相關(guān)java通過URL類下載圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于Java生成GUID的實(shí)現(xiàn)方法

    基于Java生成GUID的實(shí)現(xiàn)方法

    本篇文章是對Java生成GUID的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 詳解springboot之jackson的兩種配置方式

    詳解springboot之jackson的兩種配置方式

    這篇文章主要介紹了詳解springboot之jackson的兩種配置方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • 關(guān)于ElasticSearch的常用增刪改查DSL和代碼

    關(guān)于ElasticSearch的常用增刪改查DSL和代碼

    這篇文章主要介紹了關(guān)于ElasticSearch的常用增刪改查DSL和代碼,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • java如何實(shí)現(xiàn)多線程的順序執(zhí)行

    java如何實(shí)現(xiàn)多線程的順序執(zhí)行

    多線程是java的一種重要技術(shù),但是多線程的運(yùn)行是沒有絕對的順序的,那么java如何實(shí)現(xiàn)多線程的順序執(zhí)行,下面就一起來了解一下
    2021-05-05
  • 分享java打印簡單圖形的實(shí)現(xiàn)代碼

    分享java打印簡單圖形的實(shí)現(xiàn)代碼

    這篇文章主要分享給大家運(yùn)用java打印簡單圖形:三角形,菱形,四邊形,需要的朋友可以參考下
    2015-07-07
  • 關(guān)于注解FeignClient的使用規(guī)范

    關(guān)于注解FeignClient的使用規(guī)范

    這篇文章主要介紹了關(guān)于注解FeignClient的使用規(guī)范,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 徹底搞懂Java多線程(五)

    徹底搞懂Java多線程(五)

    這篇文章主要給大家介紹了關(guān)于Java面試題之多線程和高并發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • 關(guān)于Openfire集群源碼的分析

    關(guān)于Openfire集群源碼的分析

    這篇文章主要介紹了關(guān)于Openfire集群源碼的分析,內(nèi)容比較詳細(xì),具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-09-09
  • SpringCloud?Feign?傳輸Date類型參數(shù)存在誤差的問題

    SpringCloud?Feign?傳輸Date類型參數(shù)存在誤差的問題

    這篇文章主要介紹了SpringCloud?Feign?傳輸Date類型參數(shù)存在誤差的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • java實(shí)現(xiàn)肯德基收銀系統(tǒng)

    java實(shí)現(xiàn)肯德基收銀系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)肯德基收銀系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05

最新評論