国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

Java獲取url地址圖片

2019-11-14 15:40:33
字體:
供稿:網(wǎng)友
package com.listings.web.controller; import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileOutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.Date; public class CatchPic {     public static boolean saveUrlAs(String fileUrl, String savePath)/* fileUrl網(wǎng)絡(luò)資源地址 */          {                     try {                  /* 將網(wǎng)絡(luò)資源地址傳給,即賦值給url */                  URL url = new URL(fileUrl);                                     /* 此為聯(lián)系獲得網(wǎng)絡(luò)資源的固定格式用法,以便后面的in變量獲得url截取網(wǎng)絡(luò)資源的輸入流 */                  HttpURLConnection connection = (HttpURLConnection) url.openConnection();                  DataInputStream in = new DataInputStream(connection.getInputStream());                                     /* 此處也可用BufferedInputStream與BufferedOutputStream  需要保存的路徑*/                  DataOutputStream out = new DataOutputStream(new FileOutputStream(savePath));                                                        /* 將參數(shù)savePath,即將截取的圖片的存儲在本地地址賦值給out輸出流所指定的地址 */                  byte[] buffer = new byte[4096];                  int count = 0;                  while ((count = in.read(buffer)) > 0)/* 將輸入流以字節(jié)的形式讀取并寫入buffer中 */                  {                      out.write(buffer, 0, count);                  }                  out.close();/* 后面三行為關(guān)閉輸入輸出流以及網(wǎng)絡(luò)資源的固定格式 */                  in.close();                  connection.disconnect();                  return true;/* 網(wǎng)絡(luò)資源截取并存儲本地成功返回true */                     } catch (Exception e) {                  System.out.PRintln(e + fileUrl + savePath);                  return false;              }          }                 public static void main(String[] args) {              CatchPic pic = new CatchPic();/* 創(chuàng)建實例 */                             //需要下載的URL              String photoUrl = "http://photos.listhub.net/GAMLS/07442715/25?lm=20150426T002920";                     // 截取最后/后的字符串              String fileName = new Date().getTime()+".png";                             //圖片保存路徑              String filePath = "D:/img/";                             /* 調(diào)用函數(shù),并且進行傳參 */              boolean flag = pic.saveUrlAs(photoUrl, filePath + fileName);                             System.out.println("Run ok!/n Get URL file " + flag);              System.out.println(filePath);              System.out.println(fileName);          }   }

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄冈市| 赫章县| 巍山| 郎溪县| 南川市| 黑水县| 余姚市| 银川市| 汶上县| 邛崃市| 浦北县| 宜君县| 资阳市| 郴州市| 商洛市| 鸡泽县| 通山县| 双江| 龙门县| 长乐市| 桑植县| 泽库县| 通榆县| 资兴市| 贺州市| 岑巩县| 英超| 鄂托克旗| 正阳县| 冀州市| 苏尼特左旗| 徐汇区| 青冈县| 吉安县| 友谊县| 台南县| 诏安县| 华亭县| 长岛县| 泸水县| 宾阳县|