一步一步學會http擷取tomcat服務端的圖片,在android用戶端顯示,
最簡單的利用服務端來下載圖片到用戶端上面,剛開始接觸,記錄一下,同時希望協助到新人。
在看本篇文章之前,你可以先看下這兩篇文章
載入web項目時報的錯誤:Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modul
http://blog.csdn.net/harryweasley/article/details/45723443
Eclipse無法啟動tomcat
http://blog.csdn.net/harryweasley/article/details/45723437
轉載請註明出處:http://blog.csdn.net/harryweasley/article/details/45840523謝謝。
服務端操作步驟:
我用的編輯器是java EE Eclipse整合tomcat6.0的,:
建立檔案:File-->New -->Dynamic Web Project,然後:
注意:這裡的Dynamic web module version我選的是2.5,關於原因,你可以查看http://blog.csdn.net/harryweasley/article/details/45723443
然後點擊Finish。
然後複製兩個圖片到WebContent下,最終效果:
運行服務端,看是否有問題。
點擊項目,右鍵,Run As--->Run on Server,執行結果:
不過不要擔心,去掉Test/,則變成了
如果http://localhost:8080/還是404的話,你可以點擊http://blog.csdn.net/harryweasley/article/details/45723437查看
解決以上問題後,輸入http://localhost:8080/Test/battery.png這個網址後,顯示的內容應該是圖片的內容。
關於本機ip地址,你可以開啟電腦cmd,輸入ipconfig查看,當前ip地址
我的本地ip地址為192.168.1.48,所以,當我輸入以下地址,也是可以顯示同樣的效果的:
用戶端操作步驟:用戶端:
有兩個按鈕,顯示在螢幕,則將test.jpg圖片顯示到螢幕上,如所示,儲存到手機,則battery.bnp圖片儲存到手機目錄中。關鍵代碼如下所示:
package com.example.animal;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class HttpGetActivity extends Activity {ImageView img;Button btn, save;URL url;Bitmap bitmap;HttpURLConnection httpURLConnection;InputStream is;FileOutputStream fos;Runnable run = new Runnable() {@Overridepublic void run() {String urlname = "http://192.168.1.48:8080/Test/test.jpg";try {url = new URL(urlname);httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setConnectTimeout(3000);//允許輸入資料流httpURLConnection.setDoInput(true);//設定串連方式為GET,預設就是get請求方式httpURLConnection.setRequestMethod("GET");int responseCode = httpURLConnection.getResponseCode();// 狀態代碼等於200,表示正確if (responseCode == 200) {is = httpURLConnection.getInputStream();}bitmap = BitmapFactory.decodeStream(is);Log.i("測試", bitmap + "");handler.sendEmptyMessage(0);} catch (Exception e) {e.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}};Runnable run2 = new Runnable() {@Overridepublic void run() {String urlname = "http://192.168.1.48:8080/Test/test.jpg";try {url = new URL(urlname);httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setConnectTimeout(3000);httpURLConnection.setDoInput(true);httpURLConnection.setRequestMethod("GET");int responseCode = httpURLConnection.getResponseCode();// 狀態代碼等於200,表示正確if (responseCode == 200) {is = httpURLConnection.getInputStream();}File file=new File("sdcard/test/");file.mkdirs();fos = new FileOutputStream("sdcard/test/test.jpg");byte[] date = new byte[1024];int len;while ((len = is.read(date)) != -1) {fos.write(date, 0, len);}} catch (Exception e) {e.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (fos != null) {try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}};StringBuffer sb;Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {img.setImageBitmap(bitmap);};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);img = (ImageView) findViewById(R.id.frame_image);btn = (Button) findViewById(R.id.run);save = (Button) findViewById(R.id.save);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {new Thread(run).start();}});save.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(run2).start();}});}}