Step-by-step learn http to get Tomcat server pictures on Android client display

Source: Internet
Author: User

The simplest use of the server to download images to the client, just beginning to contact, record, and hope to help new people.

Before reading this article, you can read these two articles first


Error loading Web project times: Tomcat version 6.0 only supports EE 1.2, 1.3, 1.4, and Java EE 5 Web Modul

http://blog.csdn.net/harryweasley/article/details/45723443

Eclipse cannot start Tomcat

http://blog.csdn.net/harryweasley/article/details/45723437


Reprint Please specify source: http://blog.csdn.net/harryweasley/article/details/45824295 Thank you.

Service-side Operation steps:

The editor I used is the Java EE Eclipse integrated tomcat6.0:



Create file: File-->new-->dynamic Web Project, and then:



Note: Here's the Dynamic Web module version I chose is 2.5, for the reason you can view http://blog.csdn.net/harryweasley/article/details/45723443

then click Finish.


Then copy two pictures to WebContent, the final effect:



Run the service side to see if there is a problem.

Click on Project, right button, run as--->run on Server, execute result:



But don't worry, get rid of test/, it becomes



If http://localhost:8080/is still 404, you can click http://blog.csdn.net/harryweasley/article/details/45723437 to view



After solving the above problems, after entering http://localhost:8080/Test/battery.png this URL, the content of the image should be displayed.




About the native IP address, you can open the computer cmd, enter ipconfig view, the current IP address

My local IP address is 192.168.1.48, so I can show the same effect when I enter the following address:



Client operation steps: Client:
There are two buttons that appear on the screen, then the test.jpg picture is displayed on the screen, as shown, and saved to the phone, then the BATTERY.BNP picture is saved to the phone directory. The key code is as follows:
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);//Allow input stream httpurlconnection.setdoinput (true);//Set connection mode to get, The default is GET request mode Httpurlconnection.setrequestMethod ("GET"); int responsecode = Httpurlconnection.getresponsecode ();//status code equals 200, indicates correct if (Responsecode = =) {is = Httpurlconnection.getinputstream ();} Bitmap = Bitmapfactory.decodestream (IS); LOG.I ("Test", 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 (+); Httpurlconnection.setdoinput (true); Httpurlconnection.setrequestmethod ("GET"); int responsecode = Httpurlconnection.getresponsecode ();//status code equals 200, Indicates the correct if (Responsecode = =) {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 ();}});}} 




Step-by-step learn http to get Tomcat server pictures on Android client display

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.