熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java 讀取網絡照片

2013-11-23 18:49:26  來源: Java核心技術 

  import javaioBufferedInputStream;

  import javaioBufferedOutputStream;

  import javaioFileOutputStream;

  import HttpURLConnection;

  import URL;

  import URLConnection;

  /* *讀取網絡照片保存到本地

  * */

  public class GetUrlImg {

  public static void getUrlImg(String URLNameString target) throws Exception {//URLName照片地址

  // target本地地址

  int HttpResult = ; //服務器返回的狀態

  URL url = new URL(URLName); //創建URL

  URLConnection urlconn = urlopenConnection(); // 試圖連接並取得返回狀態碼nnect();

  HttpURLConnection httpconn = (HttpURLConnection) urlconn;

  HttpResult = ();

  Systemoutprintln(HttpResult);

  if (HttpResult != HttpURLConnectionHTTP_OK) // 不等於HTTP_OK說明連接不成功Systemoutprint(fail);

  else {

  int filesize = urlconngetContentLength(); // 取數據長度Systemoutprintln(filesize);

  BufferedInputStream bis=new BufferedInputStream(urlconngetInputStream());

  BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(target));

  byte[] buffer = new byte[]; //創建存放輸入流的緩沖

  int num = ; //讀入的字節數

  while (true) {

  num = bisread(buffer); // 讀入到緩沖區

  if (num ==){

  bosflush();

  break; //已經讀完

  }

  bosflush();

  boswrite(buffernum);

  }

  bosclose();

  bisclose();

  }

  }

  public static void main(String[] args) throws Exception{

  GetUrlImggetUrlImg();

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25804.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.