getimage.java

来自「移动商务平台」· Java 代码 · 共 86 行

JAVA
86
字号
/**
 *
 ******************************************
 ***  to Kristy,SUN YAT-SEN UNIVERSITY  *** 
 ******************************************  
 *
 *mTrade.java
 *Create on November,2003
 *Company:LeftBank
 *
 *@author Ben,XIDIAN UNIVERSITY
 *@version 0.1
 *
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

import javax.microedition.io.*;
import java.io.*;


/**
 *this class downloads image from server 
 */  
public class GetImage 
{
     private String url;
     private String picName;
     RecordStore rs = null;
     mTrade mtrade;

     GetImage(mTrade mtrade,String url,String picName)
     {
          this.url = url;
          this.picName = picName;
          this.mtrade = mtrade;
          /**
           *creat a RecordStore if it doesn't exist,then add picName to it
           *data structure in rs:picName1->pic1->picName2->pic2.... 
           */           
          try
          {
               rs = RecordStore.openRecordStore(mtrade.rsName,true);
               byte[] b = picName.getBytes();
               rs.addRecord(b,0,b.length);
          }
          catch (Exception e)
          {
              System.out.println("Record Error!"); 
          }
     }

     public void download()
     {
          /**connect the server,download images and save it in RecordStore*/
          try
          {
               Alert alert = new Alert("info","downloading data,please wait..",null,AlertType.INFO);
               alert.setTimeout(Alert.FOREVER);
              mtrade.display.setCurrent(alert);



               DataInputStream ins = Connector.openDataInputStream(url);
               ByteArrayOutputStream baos = new ByteArrayOutputStream();
               int aByte;
               while((aByte = ins.read())!=-1)
               {
                    baos.write(aByte); 
               }
               byte[] b = baos.toByteArray();
               rs.addRecord(b,0,b.length);
               ins.close();
               baos.close();
          }
          catch(Exception e)
          {
               Alert alert = new Alert("Error","download Failled..",null,AlertType.ERROR);
               alert.setTimeout(Alert.FOREVER);
               mtrade.display.setCurrent(alert);
          }
     }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?