Java通過URL類下載圖片的實(shí)例代碼
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)文章
關(guān)于ElasticSearch的常用增刪改查DSL和代碼
這篇文章主要介紹了關(guān)于ElasticSearch的常用增刪改查DSL和代碼,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04java如何實(shí)現(xiàn)多線程的順序執(zhí)行
多線程是java的一種重要技術(shù),但是多線程的運(yùn)行是沒有絕對的順序的,那么java如何實(shí)現(xiàn)多線程的順序執(zhí)行,下面就一起來了解一下2021-05-05關(guān)于注解FeignClient的使用規(guī)范
這篇文章主要介紹了關(guān)于注解FeignClient的使用規(guī)范,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SpringCloud?Feign?傳輸Date類型參數(shù)存在誤差的問題
這篇文章主要介紹了SpringCloud?Feign?傳輸Date類型參數(shù)存在誤差的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03java實(shí)現(xiàn)肯德基收銀系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)肯德基收銀系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05