冰浪

哥已不再年轻 - 坚定梦想,毕生追求!
posts - 85, comments - 90, trackbacks - 0, articles - 3
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

J2ME中通过Http协议传输图片

Posted on 2009-04-09 09:36 冰浪 阅读(222) 评论(0)  编辑  收藏 所属分类: J2ME
其实传输图片和传输其他的数据没有什么区别只是我们选择怎样的处理方法,如果我们传输java基本数据类型或者String那么比较容易,直接writeInt() readInt()等方法就可以了。如果是传输一整个对象比如一个人的信息,那么可以使用序列化把它拆开为按照一定的顺序传输多个java的基本类型和String。至于图片显得要特殊一点,因为它是二进制的文件,Java中的InputStream提供了方法来读取二进制文件,如果你对此方面的知识不熟悉请参考使用Java操作二进制文件

    在我们联网的时候同样还是要在另外一个线程进行,为了提高效率我们使用wait()和notify()来调度线程,线程启动后会进入wait()的状态,因为我们在midlet对象上调用了wait()。当用户按了Connect Command后我们调用midlet.notify()来让线程继续运行。
        if (cmd == connCommand)
        {
            synchronized (this)
            {
                notify();
            }
        } else if (cmd == exitCommand)
        {
            exitMIDlet();
        }

            synchronized (midlet)
            {
                try
                {
                    midlet.wait();
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            System.out.println("connect to server...");

    当读取Image文件的时候,我们建立连接后就可以得到InputStream的实例了,接收数据显得比较重要。我采用的方法是新建一个ByteArrayOutputStream实例baos,然后通过read()得到字节首先写入到baos里面去。传输结束后通过baos.toByteArray()得到Image的字节数据,这样我们就可以很容易的构建出图片来了,最后把它显示在Form里面。
                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.length);
                midlet.setImage(image);
                baos.close();
                is.close();
                httpConn.close();
    首先你应该在web服务器上放一个大小适中的png格式的图片,然后运行本程序进行联网,这样就可以通过http协议传输图片了。

 

 

 

 

 

 

下面是源代码

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;

public class ImageGetter extends MIDlet implements CommandListener
{

    private Display display;
    public static final Command connCommand = new Command("Connect",
            Command.ITEM, 1);
    public static final Command exitCommand = new Command("Exit", Command.EXIT,
            1);
    private Form mainForm;
    private GetterThread gt;

    protected void startApp() throws MIDletStateChangeException
    {

        display = Display.getDisplay(this);
        mainForm = new Form("Image Getter");
        mainForm.append("Click Connect to get Image");
        mainForm.addCommand(connCommand);
        mainForm.addCommand(exitCommand);
        mainForm.setCommandListener(this);
        display.setCurrent(mainForm);
        gt = new GetterThread(this);
        gt.start();

    }

    public void setImage(Image image)
    {

        mainForm.append(image);
        display.setCurrent(mainForm);
    }

    protected void pauseApp()
    {

    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException
    {

    }

    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.length);
                midlet.setImage(image);
                baos.close();
                is.close();
                httpConn.close();

            } catch (IOException e)
            {
                e.printStackTrace();
            }

        }
    }

}

(转于http://azi.javaeye.com/blog/194461)

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问