JAVAはhttpプロトコルGETメソッドでサーバから画像を取得してローカルに保存する

4276 ワード

本文の重点:
1.JavaインタフェースによるHttpプログラミング
2.GET方式でデータを要求する
3.InputStreamとFileOutputStreamの使い方を理解する
------------------------------------------------------------------------------------------------------------
この例では、
InputStream:サーバからimageの入力ストリームを返します.
FileOutputStream:image入力ストリームをローカルディスクに書き込みます.
------------------------------------------------------------------------------------------------------------
注意:
1.クライアント:eclipseを使用してjavaプロジェクトを作成する:java_http_get , /src/HttpUtils.java  (目標:javaインタフェースでHttp GET要求を実現サーバからpic 1.jpgをダウンロードしてローカルDディスクに保存する)
2.サーバー側:MyEclipseを使用して、Web Project:myhttpを作成し、/WebRootディレクトリの下にpic 1.jpgを1枚置く
3.サーバを起動し、クライアントを再実行すると、プログラム結果:サーバ上pic 1.jpgをローカルDディスクに書き込む.
------------------------------------------------------------------------------------------------------------
3枚の図.
1.クライアントjava projectディレクトリ
JAVA用http协议GET方法从服务器获取图片保存到本地_第1张图片
2.サーバ側pic 1.jpgの位置
JAVA用http协议GET方法从服务器获取图片保存到本地_第2张图片
3.プログラム実行の結果
JAVA用http协议GET方法从服务器获取图片保存到本地_第3张图片
----------------------------------------------------------------------------------------------------------------------
クライアントHttpUtils.java  コード:
package com.http.get;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpUtils {

	public static String URL_PATH = "http://192.168.0.100:8080/myhttp/pic1.jpg";

	public HttpUtils() {
		// TODO Auto-generated constructor stub
	}

	//             InputStream      
	public static void saveImageToDisk() {

		InputStream inputStream = getInputStream();
		byte[] data = new byte[1024];
		int len = 0;
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream("D:\\test1.jpg");
			while ((len = inputStream.read(data)) != -1) {
				fileOutputStream.write(data, 0, len);

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}

	}

	//            (            image   )
	public static InputStream getInputStream() {
		InputStream inputStream = null;
		HttpURLConnection httpURLConnection = null;

		try {
			URL url = new URL(URL_PATH);
			httpURLConnection = (HttpURLConnection) url.openConnection();
			//           
			httpURLConnection.setConnectTimeout(3000);
			//                 
			httpURLConnection.setDoInput(true);

			httpURLConnection.setRequestMethod("GET");
			int responseCode = httpURLConnection.getResponseCode();
			if (responseCode == 200) {
				//            
				inputStream = httpURLConnection.getInputStream();

			}

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return inputStream;

	}

	public static void main(String args[]) {
		//          ,     
		saveImageToDisk();
	}
}