隨筆 - 20  文章 - 2  trackbacks - 0
          <2009年3月>
          22232425262728
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(1)

          隨筆檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          import java.io.IOException;
          import java.awt.image.BufferedImage;
          import java.net.URL;
          import java.io.BufferedInputStream;
          import java.io.OutputStream;
          import java.io.FileOutputStream;
          import java.io.File;
          import javax.imageio.ImageIO;

          public class Img2 {
          //讀取遠程url圖片,得到寬高
              public int[] returnImgWH(String imgurl) {
                  boolean b=false;
                  try {
                      //實例化url
                      URL url = new URL(imgurl);
                      //載入圖片到輸入流
                      java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());
                      //實例化存儲字節數組
                      byte[] bytes = new byte[100];
                      //設置寫入路徑以及圖片名稱
                      OutputStream bos = new FileOutputStream(new File( "C:\\thetempimg.gif"));
                      int len;
                      while ((len = bis.read(bytes)) > 0) {
                          bos.write(bytes, 0, len);
                      }
                      bis.close();
                      bos.flush();
                      bos.close();
                      //關閉輸出流
                      b=true;
                  } catch (Exception e) {
                      //如果圖片未找到
                      b=false;
                  }
                  int[] a = new int[2];
                  if(b){    //圖片存在
                      //得到文件
                      java.io.File file = new java.io.File("C:\\thetempimg.gif");
                      BufferedImage bi = null;
                      try {
                          //讀取圖片
                          bi = javax.imageio.ImageIO.read(file);
                      } catch (IOException ex) {
                          ex.printStackTrace();
                      }
                      a[0] = bi.getWidth(); //獲得 寬度
                      a[1] = bi.getHeight(); //獲得 高度
                      //刪除文件
                      file.delete();
                  }else{     //圖片不存在
                      a=null;
                  }
                 return a;

              }

              public static void main(String[] args) {
                  Img2 i = new Img2();
                  int[] a=i.returnImgWH("        if(a==null){
                      System.out.println("圖片未找到!");
                  }else{
                      System.out.println("寬為" + a[0]);
                      System.out.println("高為" + a[1]);
                  }
              }
          }


          文章來源:
          http://wxq594808632.blog.163.com/blog/static/10907975520092274458465
          posted on 2009-03-27 16:05 武志強 閱讀(2372) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 连城县| 柳河县| 台中县| 双峰县| 晴隆县| 奉化市| 宜宾市| 巨野县| 九龙坡区| 瑞安市| 泰宁县| 平利县| 高清| 齐河县| 枣庄市| 福州市| 云梦县| 尤溪县| 新河县| 莎车县| 比如县| 江达县| 泰兴市| 吴桥县| 沁水县| 垣曲县| 华蓥市| 阳泉市| 谷城县| 温泉县| 南澳县| 哈巴河县| 邵东县| 天峻县| 光泽县| 金溪县| 思南县| 陇川县| 林口县| 泰安市| 丽水市|