一步一步學會http擷取tomcat服務端的圖片,在android用戶端顯示,

來源:互聯網
上載者:User

一步一步學會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();}});}}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.