實在傳輸圖片和傳輸其他的數據沒有什麼差別只是我們選擇怎樣的處理方法,假如我們傳輸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();
}
}
}
}