騎豬闖天下

          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 騎豬闖天下 閱讀(734) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 新源县| 交城县| 亳州市| 东明县| 溧水县| 自贡市| 兴海县| 托克逊县| 东海县| 永春县| 彭山县| 雷州市| 曲松县| 那曲县| 剑川县| 安多县| 沂源县| 赞皇县| 黄陵县| 宜兴市| 德江县| 通化县| 丁青县| 宣武区| 惠水县| 肇源县| 梁平县| 乌海市| 永安市| 阳新县| 乌什县| 金山区| 邹平县| 成都市| 凯里市| 廉江市| 阜宁县| 阿拉善盟| 武城县| 盐城市| 舒兰市|