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

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

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

    posts - 93,  comments - 2,  trackbacks - 0
    例子如下:
      1 File file = new File(filePath);
      2             if(file.exists()){
      3                 String END = "\r\n";
      4                 String PREFIX = "--" ;
      5                 String BOUNDARY =  UUID.randomUUID().toString();  //邊界標識   隨機生成
      6                 String CONTENT_TYPE = "multipart/form-data";   //內容類型
      7                 
      8                 InputStream inputStream = null;
      9                 FileInputStream fileStream = null;
     10                 DataOutputStream dos = null;
     11                 HttpURLConnection conn = null;
     12                 try {
     13                     URL url = new URL(Constant.URL_SECONDHAN_TAKEPHOTOATHAND);
     14                     conn = (HttpURLConnection) url.openConnection();
     15                     /*設置時間*/
     16                     conn.setReadTimeout(10*1000);
     17                     conn.setConnectTimeout(10*1000);
     18                     
     19                     /*允許Input,output,不使用Cache*/
     20                     conn.setDoInput(true);
     21                     conn.setDoOutput(true);
     22                     conn.setUseCaches(false);
     23                     
     24                     /*設置傳送的method=POST*/
     25                     conn.setRequestMethod("POST");
     26                     conn.setRequestProperty("Connection", "Keep-Alive");
     27                     conn.setRequestProperty("Charset", "UTF-8");
     28                     conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY); 
     29                     
     30                     /* 設置DataOutputStream */
     31                     dos = new DataOutputStream(conn.getOutputStream());
     32                     
     33                     StringBuffer sb = new StringBuffer();
     34                     sb.append(PREFIX);
     35                     sb.append(BOUNDARY);
     36                     sb.append(END);
     37                     /**
     38                      * 這里重點注意:
     39                      * name里面的值為服務器端需要key   只有這個key 才可以得到對應的文件
     40                      * filename是文件的名字,包含后綴名的   比如:abc.png  
     41                      */
     42                     sb.append("Content-Disposition: form-data; name=\"UpLoadFile\"; filename=\""+file.getName()+"\""+END); 
     43                     sb.append("Content-Type: application/x-zip-compressed; charset=utf-8"+END);
     44                     sb.append(END);
     45                     dos.write(sb.toString().getBytes());
     46                     
     47                     /*取得文件的FileInputStream*/
     48                     fileStream = new FileInputStream(file);
     49                     
     50                     /*設置每次寫入1024bytes*/
     51                     int bufferSize = 1024;
     52                     byte[] buffer = new byte[bufferSize];
     53                     int length = -1;
     54                     
     55                     /*從文件讀取數據至緩沖區*/
     56                     while((length=fileStream.read(buffer))!=-1){
     57                         dos.write(buffer,0,length);
     58                     }
     59                     dos.write(END.getBytes());
     60                     byte[] end_data = (PREFIX+BOUNDARY+PREFIX+END).getBytes();
     61                     dos.write(end_data);
     62                     dos.flush();
     63                     
     64                     /* 取得Response內容 */
     65                     InputStream is = conn.getInputStream();
     66                     int ch;
     67                     StringBuffer b =new StringBuffer();
     68                     while( ( ch = is.read() ) != -1 ){
     69                         b.append( (char)ch );
     70                     }
     71                     String result = b.toString();
     72                     
     73                 } catch (SocketTimeoutException e) {
     74                     AgUtils.log("", " upload SocketTimeoutException");
     75                     mHandler.sendEmptyMessage(4);
     76                     e.printStackTrace();
     77                 } catch (ConnectTimeoutException e) {
     78                     AgUtils.log("", " download ConnectTimeoutException");
     79                     mHandler.sendEmptyMessage(4);
     80                     e.printStackTrace();
     81                 } catch (UnknownHostException e) {
     82                     AgUtils.log("", " download UnknownHostException");
     83                     mHandler.sendEmptyMessage(4);
     84                     e.printStackTrace();
     85                 } catch (MalformedURLException e) {
     86                     AgUtils.log("", " download MalformedURLException");
     87                     mHandler.sendEmptyMessage(4);
     88                     e.printStackTrace();
     89                 } catch (ProtocolException e) {
     90                     AgUtils.log("", " ProtocolException");
     91                     e.printStackTrace();
     92                     mHandler.sendEmptyMessage(2);
     93                 } catch (IOException e) {
     94                     AgUtils.log("", " IOException");
     95                     mHandler.sendEmptyMessage(0);
     96                     e.printStackTrace();
     97                 } finally {
     98                     XqshService.this.sendBroadcast(new Intent(INTENT_UPLOAD_COMPLETE));
     99                     try {
    100                         if (fileStream != null) {
    101                             fileStream.close();
    102                         }
    103                         if (inputStream != null) {
    104                             inputStream.close();
    105                         }
    106                         if (dos != null) {
    107                             dos.close();
    108                         }
    109                         
    110                         if (conn != null) {
    111                             conn.disconnect();
    112                             conn = null;
    113                         }
    114                     } catch (IOException e) {
    115                         e.printStackTrace();
    116                     }
    117                 }
    118             }
    posted on 2014-09-11 08:16 Terry Zou 閱讀(180) 評論(0)  編輯  收藏 所屬分類: Android
    <2014年9月>
    31123456
    78910111213
    14151617181920
    21222324252627
    2829301234
    567891011

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊

    收藏夾

    Java

    搜索

    •  

    最新隨筆

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免费夜色污私人影院网站| 最近中文字幕免费完整| 午夜网站免费版在线观看| 亚洲AV色吊丝无码| 波多野结衣免费在线观看| 亚洲人成网网址在线看| 欧亚精品一区三区免费| 亚洲日本VA午夜在线影院| 国产小视频免费观看| 麻豆69堂免费视频| 国产亚洲精品无码拍拍拍色欲 | 亚洲色偷偷综合亚洲AV伊人| 日韩精品无码永久免费网站| 久久亚洲国产成人精品无码区| 免费在线黄色电影| 亚洲黄色免费电影| 欧美男同gv免费网站观看| 亚洲av成人一区二区三区在线播放| 亚洲AV无码乱码精品国产| 中文字幕不卡免费高清视频| 亚洲AV无码乱码国产麻豆| 四虎精品视频在线永久免费观看| 亚洲一区精彩视频| 免费久久精品国产片香蕉| 亚洲福利电影一区二区?| 在线观看免费高清视频| 高潮内射免费看片| 亚洲AV无码不卡在线播放| 成人免费视频69| 青青青视频免费观看| 亚洲欧洲日产国产综合网| 成人无码区免费视频观看| 人妻巨大乳hd免费看| 亚洲国产一区二区a毛片| 在线观看国产情趣免费视频 | 亚洲一级毛片免费看| 国产免费久久精品久久久| 一级毛片免费观看| 在线播放亚洲精品| 久久亚洲精品中文字幕| 成年免费a级毛片免费看无码|