一、URL類和InputStream流讀取圖片資源
Java程序可以通過URL類讀取HTTP和FTP協議的文件資源。可以通過以下代碼獲取輸入流,然後通過流讀取圖片數據,將獲取到的圖片數據存入到本地文件中。
import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; public class DownloadImage{ public static void download(String urlString, String filename) throws Exception { // 構造URL URL url = new URL(urlString); // 打開連接 InputStream inputStream = url.openConnection().getInputStream(); // 建立文件 File file=new File(filename); FileOutputStream outputStream = new FileOutputStream(file); // 讀取數據 byte[] buffer = new byte[1024]; int len = 0; while((len = inputStream.read(buffer)) != -1){ outputStream.write(buffer,0,len); } // 關閉流 inputStream.close(); outputStream.close(); } public static void main(String[] args) throws Exception { download("https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg", "D:\\images\\tree.jpg"); } }
在上述代碼示例中,download方法接收兩個參數:資源的URL和保存的文件名。程序首先構造URL,然後打開連接獲取輸入流。然後建立存儲圖片信息的文件,開啟輸出流將讀取到的數據寫入到指定文件中,最後關閉流並退出程序。
二、使用URLConnection setRequestProperty方法設置瀏覽器請求頭信息
有些URL需要在瀏覽器中正確的訪問,因此需要在Java程序中設置請求頭信息,這可以使用URLConnection類的setRequestProperty方法實現。以下是示例代碼:
import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class DownloadImage{ public static void download(String urlString, String filename) throws Exception { // 構造URL URL url = new URL(urlString); // 打開連接 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); // 設置請求頭信息,偽裝成瀏覽器發起請求 httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // 獲取流 InputStream inputStream = httpURLConnection.getInputStream(); // 建立文件 File file=new File(filename); FileOutputStream outputStream = new FileOutputStream(file); // 讀取數據 byte[] buffer = new byte[1024]; int len = 0; while((len = inputStream.read(buffer)) != -1){ outputStream.write(buffer,0,len); } // 關閉流 inputStream.close(); outputStream.close(); } public static void main(String[] args) throws Exception { download("https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg", "D:\\images\\tree.jpg"); } }
在上述代碼中,使用HttpURLConnection類打開URL連接,並在請求頭中加入用戶代理字符串,這可以防止一些服務器屏蔽來自Java程序的請求,最後獲取流和保存數據的過程與前面的示例類似。
三、使用IOUtils工具類做代碼簡化
將文件下載到本地是一個重複的過程,可以使用一個工具類簡化代碼。Apache的commons-io將其封裝為一個IOUtils工具類,這個類提供大量常用方法,可以對文件進行許多操作,從而提高開發效率,減少重複代碼。以下是使用IOUtils工具類下載文件的示例代碼:
import org.apache.commons.io.FileUtils; import java.io.File; import java.net.URL; public class DownloadImage { public static void download(String urlString, String filename) throws Exception { URL url = new URL(urlString); File file = new File(filename); FileUtils.copyURLToFile(url, file); } public static void main(String[] args) throws Exception { download("https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg", "D:\\images\\tree.jpg"); } }
在上述代碼中,使用了Apache的commons-io庫,其中FileUtils.copyURLToFile方法將指定的URL資源下載到指定的文件中,省去了構建流、保存數據的繁瑣過程,提高代碼的簡潔性和可讀性。
四、使用第三方庫Jsoup下載圖片
除了Java自帶的庫和Apache的commons-io庫外,第三方庫jsoup也可以用來下載圖片資源。Jsoup主要用於HTML解析和DOM操作,但是它也可以用來下載圖片。以下是jsoup下載文件的示例代碼:
import org.jsoup.Jsoup; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; public class DownloadImage { public static void download(String url, String filename) throws Exception { URL imageUrl = new URL(url); File file = new File(filename); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file)); InputStream inputStream = Jsoup.connect(url).ignoreContentType(true).execute().bodyStream(); byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); inputStream.close(); } public static void main(String[] args) throws Exception { download("https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg", "D:\\images\\tree.jpg"); } }
在上述代碼中,使用了jsoup.connect方法獲取指定URL的輸出流,InputStream獲取流數據,將其存儲到指定文件中。如果需要僅僅獲取二進制數據,可以使用.ignoreContentType(true)方法,這可以保證獲取到的數據是原始數據,不會進行處理。
總結
本文介紹了Java下載圖片到本地的四種方法。第一種方法利用URL類和InputStream讀取圖片資源,並且將資源寫入到指定文件。第二種方法使用URLConnection類,並且設置請求頭信息來下載圖片資源。第三種方法簡化了代碼,使用了commons-io庫中提供的copyURLToFile方法。第四種方法使用了第三方庫Jsoup,從指定URL中獲取輸入流,將文件寫入到指定文件中。這些方法各有優缺點,在實際開發中應該根據實際需要靈活選擇。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/189187.html