Pictures via HTTP protocol in J2ME

xiaoxiao2021-03-06  50

I have seen a number of netizens asked in 9CBS asked pictures to be transmitted, so I decided to write an article to tell a question, and finally demonstrate how to solve this problem and provide source code. If you are not familiar with the operations and multi-threads, please refer to this site as follows.

Write efficient and friendly multi-threaded J2ME networking applications

Development J2ME low-level networking application

Develop J2ME networking applications

In fact, there is no difference between the transmission pictures and other data. It is just how we choose how to process, if we transmit Java basic data type or String is then easier, directly writeint () readint () and other methods can be. If it is transmitted to the entire object such as one person, you can use serialization to deliver it to transmit multiple Java base types and string in accordance with certain order. As for the picture, it seems to be special, because it is a binary file, the InputStream in Java provides a way to read binary files. If you are not familiar with this knowledge, please refer to using Java operation binary files.

In the net network, we also have to do it in another thread. In order to improve efficiency, we use Wait () and Notify () to schedule the thread, and the thread will enter the status of Wait (), because we call Wait on the MIDlet object. (). When the user presses Connect Command, we call MIDlet.notify () to let the thread continue to run. IF (cmd == conncommman) {synchronized (this) {notify ();}} else if (cmd == exitcommand) {exitmidlet ();} synchronized (midlet) {Try {midlet.wait ();} catch (InterruptedExce) e) {E.PrintStackTrace ();}} system.out.println ("Connect to Server ...");

When you read the Image file, we can get the INPUTSTREAM instance after connecting, and the received data is more important. The method I use is to create a BYTEARRAYOUTPUTSTREAM instance baos, and then get byte by reading to Baos. After the end of the transmission, you get image data by baos.tobyteaRray () so that we can easily build a picture, and finally display it in 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 (); First you should put a size in a size in a mode in the web server, then run this program for networking, so you can transfer pictures through the HTTP protocol. Below is the source code 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 imports 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 (MIDletStateChangeExcection) {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) {type {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 (ooexception e) { E.PrintStackTrace ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-116505.html

New Post(0)