今天看啥
热点:

一步一步学会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() {

		@Override
		public 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 block
						e.printStackTrace();
					}
				}
			}

		}
	};

	Runnable run2 = new Runnable() {

		@Override
		public 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 block
						e.printStackTrace();
					}
				}
				if (fos != null) {
					try {
						fos.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

		}
	};
	StringBuffer sb;
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			img.setImageBitmap(bitmap);
		};
	};

	@Override
	protected 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() {

			@Override
			public void onClick(View v) {
				new Thread(run).start();
			}
		});
		save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread(run2).start();
			}
		});

	}

}



www.bkjia.comtruehttp://www.bkjia.com/Androidjc/1002129.htmlTechArticle一步一步学会http获取tomcat服务端的图片,在android客户端显示, 最简单的利用服务端来下载图片到客户端上面,刚开始接触,记录一下,同...

相关文章

    暂无相关文章
相关搜索:

帮客评论

视觉看点