Java通過(guò)URL類(lèi)下載圖片的實(shí)例代碼
Java通過(guò)URL類(lèi)下載圖片
一、概述
URL(Uniform Resource Locator) :統(tǒng)一資源定位符,它表示 Internet 上 某一 資源 的地址。 它是一種具體的 URI ,即 URL 可以用來(lái)標(biāo)識(shí)一個(gè)資源,而且還指明了如何 locate 這個(gè)資源。 通過(guò) URL 我們可以訪問(wèn) Internet 上的各種網(wǎng)絡(luò)資源,比如最常見(jiàn)的 www , ftp 站點(diǎn)。瀏覽器通過(guò)解析給定的 URL 可以在網(wǎng)絡(luò)上查找相應(yīng)的文件或其他資源。 URL 的基本結(jié)構(gòu)由 5 部分組成: < 傳輸協(xié)議 >://< 主機(jī)名 >:< 端口號(hào) >/< 文件名 ># 片段名 ? 參數(shù)列表
二、通過(guò)URL下載圖片
HttpsURLConnection httpsURLConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
//1.創(chuàng)建URL對(duì)象
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 對(duì)象上通過(guò)方法 openConnection() 生成對(duì)應(yīng)的 URLConnection
//對(duì)象。
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.connect();
//3.獲取輸入流,并創(chuàng)建輸出流對(duì)象
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通過(guò)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請(qǐng)求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對(duì)象
URL url = new URL(imageUrl);到此這篇關(guān)于Java通過(guò)URL類(lèi)下載圖片的文章就介紹到這了,更多相關(guān)java通過(guò)URL類(lèi)下載圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud集成Eureka并實(shí)現(xiàn)負(fù)載均衡的過(guò)程詳解
這篇文章主要給大家詳細(xì)介紹了SpringCloud集成Eureka并實(shí)現(xiàn)負(fù)載均衡的過(guò)程,文章通過(guò)代碼示例和圖文講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的參考價(jià)值,需要的朋友可以參考下2023-11-11
通俗易懂的Java常見(jiàn)限流算法具體實(shí)現(xiàn)
這篇文章主要介紹了Java常見(jiàn)限流算法具體實(shí)現(xiàn)的相關(guān)資料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的實(shí)現(xiàn)原理和具體步驟,并比較了它們的優(yōu)點(diǎn)和缺點(diǎn),需要的朋友可以參考下2025-02-02
詳解idea maven項(xiàng)目如何使用lib下得jar包
這篇文章主要介紹了詳解idea maven項(xiàng)目如何使用lib下得jar包,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
SpringBoot整合Redis實(shí)現(xiàn)登錄失敗鎖定功能(實(shí)例詳解)
本文我們已經(jīng)探討如何利用Redis來(lái)實(shí)現(xiàn)鎖定賬戶的安全措施,以及通過(guò)SpringBoot整合Redis實(shí)現(xiàn)了這一功能,感興趣的朋友跟隨小編一起學(xué)習(xí)下吧2024-02-02
Java經(jīng)典設(shè)計(jì)模式之策略模式原理與用法詳解
這篇文章主要介紹了Java經(jīng)典設(shè)計(jì)模式之策略模式,簡(jiǎn)單說(shuō)明了策略模式的概念、原理并結(jié)合實(shí)例形式分析了java策略模式的具有用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08
關(guān)于spring.factories失效原因分析及解決
這篇文章主要介紹了關(guān)于spring.factories失效原因分析及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Java語(yǔ)法基礎(chǔ)之for語(yǔ)句練習(xí)
以下是對(duì)Java語(yǔ)法基礎(chǔ)中的for語(yǔ)句進(jìn)行了詳細(xì)介紹,需要的朋友可以過(guò)來(lái)參考下2013-07-07

