http協議get方法獲取圖片並保存到本地


  1. public class HttpUtils {  
  2.   
  3.         //url地址,用這個地址創建URL對象。  
  4.         private  static String url_path ="http://192.168.10.101:8080/myhttp/bg.jpg";  
  5.         //這個函數負責把獲取到的InputStream流保存到本地。  
  6.         public static void saveImageToDisk() {  
  7.             InputStream inputStream = null;  
  8.             inputStream = getInputStream();//調用getInputStream()函數。  
  9.             byte[] data = new byte[1024];  
  10.             int len = 0;  
  11.               
  12.             FileOutputStream fileOutputStream = null;  
  13.             try {  
  14.                 fileOutputStream = new FileOutputStream("D:\\test.jpg");//初始化一個FileOutputStream對象。  
  15.                 while ((len = inputStream.read(data))!=-1) {//循環讀取inputStream流中的數據,存入文件流fileOutputStream  
  16.                     fileOutputStream.write(data,0,len);  
  17.                 }  
  18.                   
  19.             } catch (FileNotFoundException e) {  
  20.                 // TODO Auto-generated catch block  
  21.                 e.printStackTrace();  
  22.             } catch (IOException e) {  
  23.                 // TODO Auto-generated catch block  
  24.                 e.printStackTrace();  
  25.             }finally{//finally函數,不管有沒有異常發生,都要調用這個函數下的代碼  
  26.                 if(fileOutputStream!=null){  
  27.                     try {  
  28.                         fileOutputStream.close();//記得及時關閉文件流  
  29.                     } catch (IOException e) {  
  30.                         // TODO Auto-generated catch block  
  31.                         e.printStackTrace();  
  32.                     }  
  33.                 }  
  34.                 if(inputStream!=null){  
  35.                     try {  
  36.                         inputStream.close();//關閉輸入流  
  37.                     } catch (IOException e) {  
  38.                         // TODO Auto-generated catch block  
  39.                         e.printStackTrace();  
  40.                     }  
  41.                 }  
  42.             }  
  43.               
  44.               
  45.               
  46.         }  
  47.           
  48.           
  49.         public static InputStream getInputStream(){  
  50.             InputStream inputStream = null;  
  51.             HttpURLConnection httpURLConnection = null;  
  52.             try {  
  53.                 URL url = new URL(url_path);//創建的URL  
  54.                 if (url!=null) {  
  55.                     httpURLConnection = (HttpURLConnection) url.openConnection();//打開鏈接  
  56.                     httpURLConnection.setConnectTimeout(3000);//設置網絡鏈接超時時間,3秒,鏈接失敗后重新鏈接  
  57.                     httpURLConnection.setDoInput(true);//打開輸入流  
  58.                     httpURLConnection.setRequestMethod("GET");//表示本次Http請求是GET方式  
  59.                     int responseCode = httpURLConnection.getResponseCode();//獲取返回碼  
  60.                     if (responseCode == 200) {//成功為200  
  61.                         //從服務器獲得一個輸入流  
  62.                         inputStream = httpURLConnection.getInputStream();  
  63.                     }  
  64.                 }  
  65.                   
  66.                   
  67.             } catch (MalformedURLException e) {  
  68.                 // TODO Auto-generated catch block  
  69.                 e.printStackTrace();  
  70.             } catch (IOException e) {  
  71.                 // TODO Auto-generated catch block  
  72.                 e.printStackTrace();  
  73.             }  
  74.             return inputStream;  
  75.               
  76.         }  
  77.         //主函數  
  78.         public static void main(String[] args) {  
  79.             //從服務器獲得圖片,保存到本地  
  80.             saveImageToDisk();  
  81.         }  
  82.           
  83.     }  


注:http的get方式提交請求的流程:

創建URL;

1)定義InputStream流對象,定義HttpUrlConnection對象;

2)用URL的openConnection函數打開鏈接返回HttpUrlConnection對象;

3)HttpUrlConnection對象的setConnectTimeOut函數設置鏈接超時時間;

4)HttpUrlConnection對象的setDoInput函數打開輸入流;

5)HttpUrlConnection對象的setRequestMethod函數設置鏈接方式,即GET或者POST方式;

6)HttpUrlConnection對象的getResponseCode函數返回鏈接結果,結果為int類型;

結果有這么幾種常見的可能:200,請求成功;404資源未找到;500,服務器內部錯誤;

詳見: http狀態碼/http返回碼詳解



二.服務器端

http://blog.csdn.net/u012702039/article/details/42224721

以上鏈接的基礎上,在WebContent文件夾下放置一個bg.jpg圖片,運行起來,作為服務器端。

如圖:


然后再運行客戶端程序,即可把服務器端的bg.jpg圖片保存為d盤的test.jpg文件。



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM