<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    騎豬闖天下

    J2ME隨筆,記錄成長的腳步

    統計

    留言簿(3)

    閱讀排行榜

    評論排行榜

    [J2ME]文件上傳--手機端

    程序流程

    手機上傳文件至 proxy, proxy再轉發至CMA工程

    1.手機中部分代碼如下:

      1 private HttpResponse request(Url aUrl
      2                               ,  String aReqParam
      3                               ,  Hashtable aRequestProperties
      4                               ,  byte[] aPicData)
      5     {        
      6         //send Action 
      7         this.iListener.httpListener(iInstance, HttpRequestListener.K_CREATE_CONNECT, null);
      8         
      9         //send Action 
     10         this.iCancel = false;
     11         this.iListener.httpListener(iInstance, HttpRequestListener.K_BEGIN_CANCEL, null);
     12         
     13         //建立連接
     14         if(!OpenConnection(aUrl))
     15         {
     16             return this.getErrResponse("無法建立連接.");
     17         }
     18 
     19         //send Action 
     20         this.iListener.httpListener(iInstance, HttpRequestListener.K_SENDING_DATA, null);
     21         
     22         InputStream is = null;
     23         DataOutputStream dos = null;
     24         int resCode = 0;
     25         try
     26         {
     27             if(aReqParam != null)
     28                 iHc.setRequestMethod("POST");
     29             else
     30                 iHc.setRequestMethod("GET");
     31             
     32             this.iniRequestProperties(aRequestProperties);
     33             
     34             if(aReqParam != null)
     35             {
     36                 dos = iHc.openDataOutputStream();    
     37                 if (aPicData != null)
     38                 {   //上傳圖片時的連接,以"&#"標志在proxy里分開參數和圖片
     39                     byte[] picData = aPicData;
     40                     dos.write((aReqParam+"&#").getBytes());
     41                     
     42                     //分次發送圖片********************
     43                     if(picData.length > 1000){
     44                         int totalSize = picData.length;
     45                         int sendSize = 1000;
     46                         //發送的次數
     47                         int count = (totalSize % sendSize) == 0 ? (totalSize / sendSize)
     48                                : (totalSize / sendSize + 1);
     49                         
     50                         int len;
     51                         for (int i = 0; i < count; i++)
     52                         {
     53                            len = (totalSize - i*sendSize) > sendSize ? sendSize : (totalSize - i*sendSize);
     54                            try {
     55                             dos.write(picData, sendSize*i, len);
     56                         } catch (IOException e) {
     57                             e.printStackTrace();
     58                         }
     59                        }
     60                         
     61                     }else{
     62                         dos.write(picData);
     63                     }
     64                     //********************
     65                 }else{
     66                     //不上傳圖片時的連接
     67                     dos.writeUTF(aReqParam);
     68                 }
     69             }
     70             
     71             //send Action 
     72             this.iListener.httpListener(iInstance, HttpRequestListener.K_WAITING_RESPONSE, null);
     73 
     74             resCode = iHc.getResponseCode();
     75     
     76             //do cancel 
     77             if(this.iCancel)
     78                 return null;
     79             
     80             //get http header
     81             int index = 0;
     82             this.iResHttpHeader = new Hashtable();
     83             while (true)
     84             {
     85                 String key = iHc.getHeaderFieldKey(index++);
     86                 if(key == null)
     87                 {
     88                     break;
     89                 }
     90                 
     91                 String field = iHc.getHeaderField(key);
     92                 
     93                 iResHttpHeader.put(key, field);
     94             }
     95             
     96             //send Action 
     97             this.iListener.httpListener(iInstance, HttpRequestListener.K_END_CANCEL, null);
     98 
     99             if(resCode == 200)
    100             {
    101                 is = iHc.openInputStream();
    102 
    103                 //send Action 
    104                 this.iListener.httpListener(iInstance, HttpRequestListener.K_RECEIVING_DATA, null);
    105 
    106                 String mimeType = iHc.getHeaderField("Content-Type");
    107                 
    108                 if(mimeType == null)
    109                 {
    110                     mimeType = "";
    111                     //return this.getErrResponse("invalide mime type:"+mimeType);
    112                 }
    113                 
    114                 String contectType = iHc.getHeaderField("Content-Type");
    115                 if(contectType!=null && contectType.trim().indexOf("vnd.wap.wml")>=0)
    116                 {
    117                     try{
    118                         Thread.sleep(1000);
    119                     }catch(Exception e){}
    120                     return request(aUrl, aReqParam, aRequestProperties, aPicData);
    121                 }
    122                 
    123                 if(mimeType.trim().indexOf("image/jpeg"!= -1
    124                         || mimeType.trim().indexOf("image/png"!= -1)
    125                 {
    126                     return this.getByteArrayResponse(is);
    127                 }
    128                 else if(mimeType.trim().indexOf("text/x-zhml"!= -1
    129                         || mimeType.trim().indexOf("text/plain"!= -1)
    130                 {
    131                     return this.getStreamResponse(is);
    132                 }
    133                 else
    134                 {
    135                     return this.getErrResponse("invalide mime type:"+mimeType);
    136                 }
    137             }
    138             else//if(hc.getResponseCode() == 200)
    139             {
    140                 return this.getErrResponse("ResponseCode:"+resCode);
    141             }        
    142         }
    143         catch(IllegalArgumentException e)
    144         {
    145             return this.getErrResponse("IllegalArgumentException");
    146         }
    147         catch(ConnectionNotFoundException e)
    148         {
    149             return this.getErrResponse("ConnectionNotFoundException");
    150         }
    151         catch(IOException e)
    152         {            
    153             if(iConnCount == 1)//表明是第一次失敗
    154             {
    155                 HttpResponse res = new HttpResponse();
    156                 res.iErrStr = "IOException";
    157                 res.iTwiceConn = true;
    158                 
    159                 if(this.iDebug)
    160                     System.out.println("twice!!!");
    161                 return res;
    162             }
    163             else
    164             {
    165                 return this.getErrResponse("IOException");
    166             }
    167         }
    168         catch(Exception e)
    169         {
    170             return this.getErrResponse("Exception:1");
    171         }
    172         finally
    173         {
    174             this.destroyOutputStream(dos);
    175             
    176             try             
    177             {
    178 //                if(is != null)
    179 //                {
    180 //                    is.close();
    181 //                    is = null;
    182 //                }
    183                 if (iHc != null
    184                 {
    185                     iHc.close();
    186                     iHc = null;
    187                 }
    188             } 
    189             catch (Exception e) 
    190             {
    191                 return this.getErrResponse("Exception:2");
    192             }            
    193         }
    194     }

        其中,測試建立連接的方法如下:

     1     private HttpConnection iHc = null;
     2     private boolean OpenConnection(Url aUrl)
     3     {        
     4         iHc = null;
     5         
     6         try
     7         {
     8             if(!iIsProxy)//direct connection
     9             {    
    10                 String url = aUrl.iProtocol 
    11                            + "://" 
    12                            + aUrl.iHost;
    13                 
    14                 if(aUrl.iPort != null)
    15                     url += (":" + aUrl.iPort);
    16                 if(aUrl.iPath != null)
    17                     url += ("/" + aUrl.iPath);
    18                 
    19                 iHc = (HttpConnection) Connector.open(
    20                             url
    21                             , Connector.READ_WRITE,true);
    22             }
    23             else//proxy connection
    24             {
    25                 iHc = (HttpConnection)Connector.open("http://10.0.0.172:80"
    26                         + "/"
    27                         + aUrl.iPath
    28                     , Connector.READ_WRITE, true);
    29                 
    30                 if(aUrl.iPort != null)
    31                 {
    32                     iHc.setRequestProperty("X-Online-Host"
    33                             , aUrl.iHost + ":" + aUrl.iPort);                                    
    34                 }
    35                 else
    36                 {
    37                     iHc.setRequestProperty("X-Online-Host"
    38                             , aUrl.iHost);                                                        
    39                 }
    40             }
    41             
    42             if(this.iHc != null)
    43                 return true;
    44             else
    45                 return false;
    46         }
    47         catch(Exception e)
    48         {
    49             return false;
    50         }        
    51     }

       附:沒有文件上傳的方法,如下:

        private HttpResponse request(Url aUrl
    ,  String aReqParam
    ,  Hashtable aRequestProperties)
    {        
    //send Action 
            this.iListener.httpListener(iInstance, HttpRequestListener.K_CREATE_CONNECT, null);

    //send Action 
            this.iCancel = false;
    this.iListener.httpListener(iInstance, HttpRequestListener.K_BEGIN_CANCEL, null);

    //建立連接
            if(!OpenConnection(aUrl))
    {
    return this.getErrResponse("無法建立連接.");
    }

    //send Action 
            this.iListener.httpListener(iInstance, HttpRequestListener.K_SENDING_DATA, null);

    InputStream is 
    = null;
    DataOutputStream dos 
    = null;
    int resCode = 0;
    try
    {
    if(aReqParam != null)
    iHc.setRequestMethod(
    "POST");
    else
    iHc.setRequestMethod(
    "GET");

    this.iniRequestProperties(aRequestProperties);

    if(aReqParam != null)
    {
    dos 
    = iHc.openDataOutputStream();                            
    dos.writeUTF(aReqParam);
    }

    //send Action 
                this.iListener.httpListener(iInstance, HttpRequestListener.K_WAITING_RESPONSE, null);

    resCode 
    = iHc.getResponseCode();

    //do cancel 
                if(this.iCancel)
    return null;

    //get http header
                int index = 0;
    this.iResHttpHeader = new Hashtable();
    while (true)
    {
    String key 
    = iHc.getHeaderFieldKey(index++);
    if(key == null)
    {
    break;
    }

    String field 
    = iHc.getHeaderField(key);

    iResHttpHeader.put(key, field);
    }

    //send Action 
                this.iListener.httpListener(iInstance, HttpRequestListener.K_END_CANCEL, null);

    if(resCode == 200)
    {……
    ……

     

    posted on 2009-02-11 16:00 騎豬闖天下 閱讀(732) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲AV成人无码久久精品老人| 国产午夜鲁丝片AV无码免费| 国产午夜亚洲不卡| 黄色一级毛片免费看| 国产jizzjizz视频全部免费| 亚洲AV无码一区二区三区鸳鸯影院| 日韩激情淫片免费看| 亚洲狠狠婷婷综合久久| 国产裸模视频免费区无码| 国产成人亚洲综合在线| 亚洲XX00视频| a毛片视频免费观看影院| 久久精品亚洲日本佐佐木明希| 99久久精品毛片免费播放| 亚洲精品成人av在线| 足恋玩丝袜脚视频免费网站| 亚洲性色成人av天堂| 免费高清资源黄网站在线观看| 色偷偷亚洲第一综合网| 亚洲毛片av日韩av无码| a级毛片在线免费看| 亚洲色偷偷av男人的天堂| 男男AV纯肉无码免费播放无码| 亚洲欧美熟妇综合久久久久| 免费在线观看日韩| a色毛片免费视频| 久久精品国产亚洲AV忘忧草18| 全部免费a级毛片| 少妇性饥渴无码A区免费| 亚洲沟沟美女亚洲沟沟| 免费a级毛片视频| 中文字幕乱码一区二区免费| 亚洲综合久久一本伊伊区| 国产极品美女高潮抽搐免费网站| 久久国产一片免费观看| 亚洲国产成人久久综合一区| 国产成人无码区免费A∨视频网站| rh男男车车的车车免费网站| 久久精品蜜芽亚洲国产AV| 国产在线不卡免费播放| 日本亚洲欧洲免费天堂午夜看片女人员 |