Step by step, you can learn http To Get tomcat server images, which are displayed on the android client,

Source: Internet
Author: User

Step by step, you can learn http To Get tomcat server images, which are displayed on the android client,

The simplest way is to use the server to download images to the client. At the beginning of the contact, record the images and hope to help new users.

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


Error reported when loading web project: 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 cannot start tomcat

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


For more information, see http://blog.csdn.net/harryweasley/article/details/45840523xie.

Server operation steps:

The editor I used is integrated with tomcat6.0 by java EE Eclipse ,:



Create a File: File --> New --> Dynamic Web Project, and then:



Note: The Dynamic web module version here I selected is 2.5, for the reason, you can view the http://blog.csdn.net/harryweasley/article/details/45723443

Click Finish.


Then copy the two images to the WebContent file. The final effect is as follows:



Run the server to check whether there is any problem.

Right-click the project and choose Run As ---> Run on Server. The execution result is As follows:



But don't worry. Remove Test/and it becomes



If http: // localhost: 8080/or 404, click http://blog.csdn.net/harryweasley/article/details/45723437to View



After the preceding problem is solved, enter http: // localhost: 8080/Test/battery.png to display the image content.




For the local IP address, you can open the computer cmd and enter ipconfig to view the current IP address.

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



Client operation steps: client:
When two buttons are displayed on the screen, the test.jpg image is displayed on the screen, as shown in. If the test.jpg image is saved to the mobile phone, the battery. bnp Paribas image is saved to the mobile 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. vi Ew. 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); // allows the input stream httpURLConnection. setDoInput (true); // set the connection method to GET. The default value is the get Request Method httpURLConnection. setRequestMethod ("GET"); int responseCode = httpURLConnection. getResponseCode (); // the status code is 200, indicating the correct if (responseCode = 200) {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 (3000); httpURLConnection. setDoInput (true); httpURL Connection. setRequestMethod ("GET"); int responseCode = httpURLConnection. getResponseCode (); // the status code is 200, indicating the correct 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 ();}});}}



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.