J2ME中通过Http协议传输图片[3]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

    public void commandAction(Command cmd, Displayable disp)
    {
        if (cmd == connCommand)
        {
            synchronized (this)
            {
                notify();
            }
        } else if (cmd == exitCommand)
        {
            exitMIDlet();
        }
    }

    private void exitMIDlet()
    {
        try
        {
            destroyApp(false);
            notifyDestroyed();
        } catch (MIDletStateChangeException e)
        {
            e.printStackTrace();
        }
    }

    class GetterThread extends Thread
    {
        private ImageGetter midlet;
        public static final String URL = "http://localhost/j2medev.png";
        private HttpConnection httpConn = null;
        private InputStream is = null;

        public GetterThread(ImageGetter midlet)
        {
            this.midlet = midlet;
        }

        public void run()
        {
            synchronized (midlet)
            {
                try
                {
                    midlet.wait();
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            System.out.println("connect to server...");
            try
            {
                httpConn = (HttpConnection) Connector.open(URL);
                is = httpConn.openInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int ch = 0;
                while ((ch = is.read()) != -1)
                {
                    baos.write(ch);
                }
                byte[] imageData = baos.toByteArray();
                Image image = Image.createImage(imageData, 0, imageData.l

本文关键:J2ME中通过Http协议传输图片
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top