Java将网络地址对应的图片转成本地的图片
生活随笔
收集整理的這篇文章主要介紹了
Java将网络地址对应的图片转成本地的图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package?imageView;?? import?java.io.ByteArrayOutputStream;?? import?java.io.File;?? import?java.io.FileOutputStream;?? import?java.io.InputStream;?? import?java.net.HttpURLConnection;?? import?java.net.URL;?? /**? ?*?@說明?從網絡獲取圖片到本地? ?*?@author?崔素強? ?*?@version?1.0? ?*?@since? ?*/?? public?class?GetImage?{?? ????/**? ?????*?測試? ?????*?@param?args? ?????*/?? ????public?static?void?main(String[]?args)?{?? ????????String?url?=?"http://www.baidu.com/img/baidu_sylogo1.gif";?? ????????byte[]?btImg?=?getImageFromNetByUrl(url);?? ????????if(null?!=?btImg?&&?btImg.length?>?0){?? ????????????System.out.println("讀取到:"?+?btImg.length?+?"?字節");?? ????????????String?fileName?=?"百度.gif";?? ????????????writeImageToDisk(btImg,?fileName);?? ????????}else{?? ????????????System.out.println("沒有從該連接獲得內容");?? ????????}?? ????}?? ????/**? ?????*?將圖片寫入到磁盤? ?????*?@param?img?圖片數據流? ?????*?@param?fileName?文件保存時的名稱? ?????*/?? ????public?static?void?writeImageToDisk(byte[]?img,?String?fileName){?? ????????try?{?? ????????????File?file?=?new?File("C:\\"?+?fileName);?? ????????????FileOutputStream?fops?=?new?FileOutputStream(file);?? ????????????fops.write(img);?? ????????????fops.flush();?? ????????????fops.close();?? ????????????System.out.println("圖片已經寫入到C盤");?? ????????}?catch?(Exception?e)?{?? ????????????e.printStackTrace();?? ????????}?? ????}?? ????/**? ?????*?根據地址獲得數據的字節流? ?????*?@param?strUrl?網絡連接地址? ?????*?@return? ?????*/?? ????public?static?byte[]?getImageFromNetByUrl(String?strUrl){?? ????????try?{?? ????????????URL?url?=?new?URL(strUrl);?? ????????????HttpURLConnection?conn?=?(HttpURLConnection)url.openConnection();?? ????????????conn.setRequestMethod("GET");?? ????????????conn.setConnectTimeout(5?*?1000);?? ????????????InputStream?inStream?=?conn.getInputStream();//通過輸入流獲取圖片數據?? ????????????byte[]?btImg?=?readInputStream(inStream);//得到圖片的二進制數據?? ????????????return?btImg;?? ????????}?catch?(Exception?e)?{?? ????????????e.printStackTrace();?? ????????}?? ????????return?null;?? ????}?? ????/**? ?????*?從輸入流中獲取數據? ?????*?@param?inStream?輸入流? ?????*?@return? ?????*?@throws?Exception? ?????*/?? ????public?static?byte[]?readInputStream(InputStream?inStream)?throws?Exception{?? ????????ByteArrayOutputStream?outStream?=?new?ByteArrayOutputStream();?? ????????byte[]?buffer?=?new?byte[1024];?? ????????int?len?=?0;?? ????????while(?(len=inStream.read(buffer))?!=?-1?){?? ????????????outStream.write(buffer,?0,?len);?? ????????}?? ????????inStream.close();?? ????????return?outStream.toByteArray();?? ????}?? }??
package imageView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/*** @說明 從網絡獲取圖片到本地* @author 崔素強* @version 1.0* @since*/
public class GetImage {/*** 測試* @param args*/public static void main(String[] args) {String url = "http://www.baidu.com/img/baidu_sylogo1.gif";byte[] btImg = getImageFromNetByUrl(url);if(null != btImg && btImg.length > 0){System.out.println("讀取到:" + btImg.length + " 字節");String fileName = "百度.gif";writeImageToDisk(btImg, fileName);}else{System.out.println("沒有從該連接獲得內容");}}/*** 將圖片寫入到磁盤* @param img 圖片數據流* @param fileName 文件保存時的名稱*/public static void writeImageToDisk(byte[] img, String fileName){try {File file = new File("C:\\" + fileName);FileOutputStream fops = new FileOutputStream(file);fops.write(img);fops.flush();fops.close();System.out.println("圖片已經寫入到C盤");} catch (Exception e) {e.printStackTrace();}}/*** 根據地址獲得數據的字節流* @param strUrl 網絡連接地址* @return*/public static byte[] getImageFromNetByUrl(String strUrl){try {URL url = new URL(strUrl);HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5 * 1000);InputStream inStream = conn.getInputStream();//通過輸入流獲取圖片數據byte[] btImg = readInputStream(inStream);//得到圖片的二進制數據return btImg;} catch (Exception e) {e.printStackTrace();}return null;}/*** 從輸入流中獲取數據* @param inStream 輸入流* @return* @throws Exception*/public static byte[] readInputStream(InputStream inStream) throws Exception{ByteArrayOutputStream outStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while( (len=inStream.read(buffer)) != -1 ){outStream.write(buffer, 0, len);}inStream.close();return outStream.toByteArray();}
}
只知道瀏覽器使用的是HTTP協議,那么如何將網絡資源使用JavaHTTP下載下來呢!
這只是一個非常簡單的小示例,只是不想每次碰到關于此方面的內容忘了就無從下手!
示例創建HttpURLConnection網絡連接,并將這個連接獲得的網絡數據流寫道本地磁盤!
?
示例代碼如下:
Java代碼 ??
總結
以上是生活随笔為你收集整理的Java将网络地址对应的图片转成本地的图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android学习笔记:对Android
- 下一篇: A43_太子步兵坦克