J2ME实现从服务器端下载文件(J2me for HttpConnection)

J2ME实现从服务器端下载文件:
我以下载服务器文件并在手机客户端显示图片为例:

[color=green]测试截图:[/color]
[img]http://dl.iteye.com/upload/attachment/204413/b1affc8f-90ad-39b4-b9bc-4c497e433ebe.png[/img]
[color=green]程序代码[/color]

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;

}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值