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

测试截图:

程序代码

Java代码  收藏代码
    1. package com.mopietek;  
    2.   
    3. import java.io.IOException;  
    4. import java.io.InputStream;  
    5.   
    6. import javax.microedition.io.Connector;  
    7. import javax.microedition.io.HttpConnection;  
    8. import javax.microedition.lcdui.Command;  
    9. import javax.microedition.lcdui.CommandListener;  
    10. import javax.microedition.lcdui.Display;  
    11. import javax.microedition.lcdui.Displayable;  
    12. import javax.microedition.lcdui.Form;  
    13. import javax.microedition.lcdui.Image;  
    14. import javax.microedition.lcdui.StringItem;  
    15. import javax.microedition.midlet.MIDlet;  
    16. import javax.microedition.midlet.MIDletStateChangeException;  
    17.   
    18. public class HttpMID extends MIDlet implements CommandListener,Runnable{  
    19.   
    20.     public static Command connect = new Command ("联网",Command.OK,1);  
    21.     public static Command change = new Command("更改入网的方式",Command.OK,1);  
    22.     public static Command exit = new Command("退出",Command.EXIT,1);  
    23.       
    24.     public Form form;  
    25.     public Display display;  
    26.       
    27.     HttpConnection http;  
    28.       
    29.     String host = "dev.mopietek.net:8080/ebook";  
    30.     String path = "/upload/123.gif";  
    31.     //接入点为cmnet或者是cmwap  
    32.     boolean cmnet = true;  
    33.       
    34.     public static final int WAIT = 0;  //等待  
    35.     public static final int CONNECT = 1//连接中  
    36.     public static final int SUCCESS = 2//成功  
    37.     public static final int FALL = 3//失败  
    38.     public int state; //当前状态  
    39.       
    40.     StringBuffer sb = new StringBuffer("当前接入方式为:CMNET\n");  
    41.     StringItem si = new StringItem(null,sb.toString());  
    42.       
    43.       
    44.     public HttpMID(){  
    45.           
    46.         display = Display.getDisplay(this);  
    47.         state = WAIT;  
    48.           
    49.         form = new Form("HttpTest");  
    50.         form.append(si);  
    51.         form.addCommand(connect);  
    52.         form.addCommand(change);  
    53.         form.addCommand(exit);  
    54.         form.setCommandListener(this);  
    55.           
    56.     }  
    57.       
    58.     protected void destroyApp(boolean unconditional)  
    59.             throws MIDletStateChangeException {  
    60.         // TODO Auto-generated method stub  
    61.           
    62.     }  
    63.   
    64.     protected void pauseApp() {  
    65.         // TODO Auto-generated method stub  
    66.           
    67.     }  
    68.   
    69.     protected void startApp() throws MIDletStateChangeException {  
    70.   
    71.         display.setCurrent(form);  
    72.     }  
    73.   
    74.     public void commandAction(Command c, Displayable d) {  
    75.   
    76.         if(c == change){  
    77.             if(isConnect())  
    78.                 return;  
    79.             cmnet = ! cmnet;  
    80.             form.deleteAll();  
    81.               
    82.             sb.delete(0, sb.length());  
    83.             addStr("当前接入方式为:" + (cmnet ? "CMNET" : "CMWAP"));  
    84.             form.append(si);  
    85.               
    86.         }  
    87.         else if(c == connect){  
    88.             if(isConnect())  
    89.                 return;  
    90.             new Thread(this).start();  
    91.               
    92.         }else if(c == exit){  
    93.               
    94.             try {  
    95.                 destroyApp(true);  
    96.             } catch (MIDletStateChangeException e) {  
    97.                 // TODO Auto-generated catch block  
    98.                 e.printStackTrace();  
    99.             }  
    100.             this.notifyDestroyed();  
    101.         }  
    102.           
    103.     }  
    104.   
    105.       
    106.       
    107.     private boolean isConnect() {  
    108.           
    109.         if(state == CONNECT){  
    110.            addStr("网络链接中,请稍后...");  
    111.            return true;  
    112.         }  
    113.         return false;  
    114.     }  
    115.   
    116.     private void addStr(String str) {  
    117.           
    118.         sb.append(str + "\n");  
    119.         si.setText(sb.toString());  
    120.     }  
    121.   
    122.     public void run() {  
    123.   
    124.         form.deleteAll();  
    125.           
    126.         sb.delete(0, sb.length());  
    127.         addStr("当前接入方式为:" +(cmnet ? "CMNET" : "CMWAP"));  
    128.         form.append(si);  
    129.         state = CONNECT;  
    130.         addStr("网络链接中..");  
    131.           
    132.         InputStream is = null;  
    133.           
    134.         try{  
    135.               
    136.             String url = null;  
    137.             url = cmnet ? ("http://" + host + path) : ("http://10.0.0.172:80" +path);  
    138.               
    139.             http = (HttpConnection) Connector.open(url,Connector.READ_WRITE,true);  
    140.               
    141.             if(!cmnet)  
    142.                  
    143.                 http.setRequestProperty("X-Online-Host", host);  
    144.                 http.setRequestMethod(HttpConnection.GET);  
    145.                 String contentType = http.getHeaderField("Content-Type");  
    146.                 System.out.println(contentType);  
    147.                 if(contentType != null && contentType.indexOf("text/vnd.wap.wml") != -1){  
    148.                       
    149.                      addStr("移动资费页面,过滤");  
    150.                      http.close();  
    151.                      http = null;  
    152.                      http = (HttpConnection) Connector.open(url,Connector.READ_WRITE,true);  
    153.                   
    154.                      if(!cmnet)  
    155.                          http.setRequestProperty("X-Online-Host", host);  
    156.                          http.setRequestMethod(HttpConnection.GET);  
    157.                          contentType = http.getHeaderField("Content-Type");  
    158.                 }  
    159.                   
    160.                 addStr("Content-Type=" + contentType);  
    161.                 int code = http.getResponseCode();  
    162.                 addStr("HTTP Code :" + code);  
    163.                 if(code == 200){  
    164.                     addStr("网络联网成功,接收数据...");  
    165.                     is = http.openInputStream();  
    166.                     Image image = Image.createImage(is);  
    167.                     addStr("数据接收完毕,显示图片...");  
    168.                     form.append(image);  
    169.                       
    170.                     state = SUCCESS;  
    171.                     return;  
    172.                 }else{  
    173.                     addStr("访问页面失败");  
    174.                 }  
    175.         }catch(IOException e){  
    176.             addStr("联网发生异常:" + e.toString());  
    177.             e.printStackTrace();  
    178.         }catch(Exception ee){  
    179.             addStr("发生异常:" +ee.toString());  
    180.             ee.printStackTrace();  
    181.         }finally{  
    182.             if(is != null){  
    183.                 try{  
    184.                     is.close();  
    185.                 }catch(IOException e){  
    186.                     e.printStackTrace();  
    187.                 }  
    188.                 is = null;  
    189.             }  
    190.               
    191.             if(http != null)  
    192.                 try{  
    193.                      http.close();    
    194.                 }catch(IOException e){  
    195.                     e.printStackTrace();  
    196.                 }  
    197.                 http = null;  
    198.             }  
    199.             state = FALL;  
    200.           
    201.     }  
    202.       
    203.   

posted on 2013-03-06 13:51  爱哎唉  阅读(205)  评论(0)    收藏  举报