J2ME实现从服务器端下载文件:
我以下载服务器文件并在手机客户端显示图片为例:
测试截图:
程序代码
- package com.mopietek;
- import java.io.IOException;
- import java.io.InputStream;
- import javax.microedition.io.Connector;
- import javax.microedition.io.HttpConnection;
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Form;
- import javax.microedition.lcdui.Image;
- import javax.microedition.lcdui.StringItem;
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.midlet.MIDletStateChangeException;
- public class HttpMID extends MIDlet implements CommandListener,Runnable{
- public static Command connect = new Command ("联网",Command.OK,1);
- public static Command change = new Command("更改入网的方式",Command.OK,1);
- public static Command exit = new Command("退出",Command.EXIT,1);
- public Form form;
- public Display display;
- HttpConnection http;
- String host = "dev.mopietek.net:8080/ebook";
- String path = "/upload/123.gif";
- //接入点为cmnet或者是cmwap
- boolean cmnet = true;
- public static final int WAIT = 0; //等待
- public static final int CONNECT = 1; //连接中
- public static final int SUCCESS = 2; //成功
- public static final int FALL = 3; //失败
- public int state; //当前状态
- StringBuffer sb = new StringBuffer("当前接入方式为:CMNET\n");
- StringItem si = new StringItem(null,sb.toString());
- public HttpMID(){
- display = Display.getDisplay(this);
- state = WAIT;
- form = new Form("HttpTest");
- form.append(si);
- form.addCommand(connect);
- form.addCommand(change);
- form.addCommand(exit);
- form.setCommandListener(this);
- }
- protected void destroyApp(boolean unconditional)
- throws MIDletStateChangeException {
- // TODO Auto-generated method stub
- }
- protected void pauseApp() {
- // TODO Auto-generated method stub
- }
- protected void startApp() throws MIDletStateChangeException {
- display.setCurrent(form);
- }
- public void commandAction(Command c, Displayable d) {
- if(c == change){
- if(isConnect())
- return;
- cmnet = ! cmnet;
- form.deleteAll();
- sb.delete(0, sb.length());
- addStr("当前接入方式为:" + (cmnet ? "CMNET" : "CMWAP"));
- form.append(si);
- }
- else if(c == connect){
- if(isConnect())
- return;
- new Thread(this).start();
- }else if(c == exit){
- try {
- destroyApp(true);
- } catch (MIDletStateChangeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- this.notifyDestroyed();
- }
- }
- private boolean isConnect() {
- if(state == CONNECT){
- addStr("网络链接中,请稍后...");
- return true;
- }
- return false;
- }
- private void addStr(String str) {
- sb.append(str + "\n");
- si.setText(sb.toString());
- }
- public void run() {
- form.deleteAll();
- sb.delete(0, sb.length());
- addStr("当前接入方式为:" +(cmnet ? "CMNET" : "CMWAP"));
- form.append(si);
- state = CONNECT;
- addStr("网络链接中..");
- InputStream is = null;
- try{
- String url = null;
- url = cmnet ? ("http://" + host + path) : ("http://10.0.0.172:80" +path);
- http = (HttpConnection) Connector.open(url,Connector.READ_WRITE,true);
- if(!cmnet)
- http.setRequestProperty("X-Online-Host", host);
- http.setRequestMethod(HttpConnection.GET);
- String contentType = http.getHeaderField("Content-Type");
- System.out.println(contentType);
- if(contentType != null && contentType.indexOf("text/vnd.wap.wml") != -1){
- addStr("移动资费页面,过滤");
- http.close();
- http = null;
- http = (HttpConnection) Connector.open(url,Connector.READ_WRITE,true);
- if(!cmnet)
- http.setRequestProperty("X-Online-Host", host);
- http.setRequestMethod(HttpConnection.GET);
- contentType = http.getHeaderField("Content-Type");
- }
- addStr("Content-Type=" + contentType);
- int code = http.getResponseCode();
- addStr("HTTP Code :" + code);
- if(code == 200){
- addStr("网络联网成功,接收数据...");
- is = http.openInputStream();
- Image image = Image.createImage(is);
- addStr("数据接收完毕,显示图片...");
- form.append(image);
- state = SUCCESS;
- return;
- }else{
- addStr("访问页面失败");
- }
- }catch(IOException e){
- addStr("联网发生异常:" + e.toString());
- e.printStackTrace();
- }catch(Exception ee){
- addStr("发生异常:" +ee.toString());
- ee.printStackTrace();
- }finally{
- if(is != null){
- try{
- is.close();
- }catch(IOException e){
- e.printStackTrace();
- }
- is = null;
- }
- if(http != null)
- try{
- http.close();
- }catch(IOException e){
- e.printStackTrace();
- }
- http = null;
- }
- state = FALL;
- }
- }
浙公网安备 33010602011771号