`
1140566087
  • 浏览: 547611 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
博客专栏
2c4ae07c-10c2-3bb0-a106-d91fe0a10f37
c/c++ 入门笔记
浏览量:18074
3161ba8d-c410-3ef9-871c-3e48524c5263
Android 学习笔记
浏览量:309446
Group-logo
J2ME 基础学习课程集
浏览量:17988
A98a97d4-eb03-3faf-af96-c7c28f709feb
Spring 学习过程记录...
浏览量:17193
社区版块
存档分类
最新评论

J2ME GCF--通用连接框架--图片下载

    博客分类:
  • J2ME
阅读更多
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.ContentConnection;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

// 			第六章  开发无线网络应用程序
//Generic Connection Framework --通用连接框架
//GCF的特性:
//		基于接口设计,便于扩展,提供了创建连接的工厂方法 ,使用标准的url  
//	各接口的作用:HttpConnection HttpsConnection 接口,使得MIDlet 具备了通过Http 或者Https 协议与server 通信的能力;
//	可选:SocketConnection / ServerSocketConnection、UDPDatagramConnection , 使得MIDlet 能够在TCP/IP 层通过Socket进行通信或者
//		使用数据报进行通信

//  现实世界使用的是:分组数据交换和电路交换 , 因此在联网框架中定义了 :DatagramConnection 和 StreamCononection;
//	在基于流的传输中我们要对输入流和输出流是具有操作能力的;StreamConnection 扩展了InputConnection 和OutputConnection; 打开:Con.opeanInputStream();
//	SreamConnectionNotifier接口定义了连接监听器应该具备的能力;


public class SixSection_GCF extends MIDlet {

	//准备需要使用到的数据类型
	private ContentConnection connection = null;
	private ByteArrayOutputStream baos = null; // 字节数据输出流
	private  Image image = null;  //初始化图片
	private byte[] imageData = null;
	private DataInputStream dis = null;		// 新建数据输出流
	private Form form = new Form("下载图片");
	private Display display ;
	
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stubs

	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		display = Display.getDisplay(this);
		getImage("http://localhost:8080/imageSite/mv.jpg");
		form.append(image);
		display.setCurrent(form);
	}
	
	//下载图片
	public void getImage(String url){
		try{
			//建立Http连接
			connection = (ContentConnection) Connector.open(url);
			//新建输入输入流
			dis = connection.openDataInputStream();
			//获得Http连接的长度
			int length =(int) connection.getLength();
			
			if(length!=-1){
				imageData = new byte[length];
				//使用流读取 --输入,读  /输出,写
				dis.readFully(imageData);  // 读取到数据
			}else{
				baos = new ByteArrayOutputStream();
				int ch = 0;
				while((dis.read())!=-1){
					baos.write(ch);
					imageData = baos.toByteArray();
				}
			}
			image = Image.createImage(imageData, 0, imageData.length);
			
		}catch(Exception exs){
			exs.printStackTrace();
		}finally{
			//资源回收
			try{
				if(connection!=null){connection.close();}
				if(dis!=null){
					dis.close();
				}
				if(baos!=null){
					baos.close();
				}
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics