downloadthread.java

来自「J2ME 无线通信技术应用开发源代码」· Java 代码 · 共 54 行

JAVA
54
字号
//程序名DownloadThread.java,项目DownloadMIDlet
//下载应用的综合实例
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.rms.*;
import javax.microedition.lcdui.*;

public class DownloadThread extends Thread {
	private String uri;
	private String picName;
	RecordStore rs = null;
	DownloadMIDlet midlet;
	
	DownloadThread(DownloadMIDlet midlet,String uri, String picName) {
		this.uri = uri;
		this.midlet = midlet;
		this.picName = picName;
    try {
		  rs = RecordStore.openRecordStore(midlet.rsName, true );
		  byte[] b = picName.getBytes();
			rs.addRecord(b,0,b.length);
    } catch( Exception e ){
      //处理异常情况,这里没有实现
    }
	}

  public void run() {
    try {
      HttpConnection conn = (HttpConnection)Connector.open(uri);
      InputStream in = conn.openInputStream();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      int oneByte;
      while( ( oneByte = in.read() ) != -1 ){
        baos.write(oneByte);
      }
      byte[] b = baos.toByteArray();
      rs.addRecord(b,0,b.length);
      in.close();
      baos.close();
      conn.close();
      rs.closeRecordStore();
      Alert alert = new Alert("信息", "下载成功完成\n"+picName, null, AlertType.CONFIRMATION);
      alert.setTimeout(Alert.FOREVER);
      midlet.viewList.append(picName,midlet.listIcon);
      midlet.getPicNames();
      midlet.display.setCurrent(alert,midlet.viewList);
    } catch(Exception e ){
      Alert alert = new Alert("信息", "下载失败!\n"+picName, null, AlertType.ERROR);
      alert.setTimeout(Alert.FOREVER);
      midlet.display.setCurrent(alert,midlet.viewList);
    } 
  }
}

⌨️ 快捷键说明

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