天天看點

J2ME中通過Http協定傳輸圖檔

        曾經在CSDN看到多位網友問圖檔傳輸的問題,是以決定寫篇文章講述一個問題,最後通過執行個體示範如何解決這個問題并提供源代碼。如果你對聯網中的操作以及多線程還不熟悉那麼請參考本站如下文章。

    編寫高效友好的多線程J2ME聯網應用

    開發J2ME低級聯網應用  

    開發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協定傳輸圖檔了。

J2ME中通過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();

            }

        }

    }

}