其實傳輸圖片和傳輸其他的數(shù)據(jù)沒有什么區(qū)別只是我們選擇怎樣的處理方法,如果我們傳輸java基本數(shù)據(jù)類型或者String那么比較容易,直接writeInt() readInt()等方法就可以了。如果是傳輸一整個對象比如一個人的信息,那么可以使用序列化把它拆開為按照一定的順序傳輸多個java的基本類型和String。至于圖片顯得要特殊一點,因為它是二進制的文件,Java中的InputStream提供了方法來讀取二進制文件,如果你對此方面的知識不熟悉請參考
使用Java操作二進制文件。
在我們聯(lián)網(wǎng)的時候同樣還是要在另外一個線程進行,為了提高效率我們使用wait()和notify()來調度線程,線程啟動后會進入wait()的狀態(tài),因為我們在midlet對象上調用了wait()。當用戶按了Connect Command后我們調用midlet.notify()來讓線程繼續(xù)運行。
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的實例了,接收數(shù)據(jù)顯得比較重要。我采用的方法是新建一個ByteArrayOutputStream實例baos,然后通過read()得到字節(jié)首先寫入到baos里面去。傳輸結束后通過baos.toByteArray()得到Image的字節(jié)數(shù)據(jù),這樣我們就可以很容易的構建出圖片來了,最后把它顯示在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格式的圖片,然后運行本程序進行聯(lián)網(wǎng),這樣就可以通過http協(xié)議傳輸圖片了。
下面是源代碼
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)