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 閱讀(184) 評論(0)  編輯  收藏 所屬分類: Android
          <2014年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 榆中县| 昌平区| 罗山县| 德保县| 石门县| 卓尼县| 开远市| 溧阳市| 平江县| 阿鲁科尔沁旗| 图木舒克市| 甘谷县| 邢台县| 新巴尔虎右旗| 赫章县| 林芝县| 时尚| 洛浦县| 青海省| 昌都县| 泾川县| 玛沁县| 榆中县| 宜章县| 诸城市| 阿尔山市| 商丘市| 资溪县| 彩票| 荔浦县| 高碑店市| 绥德县| 沭阳县| 桂林市| 汉沽区| 和平县| 绥阳县| 留坝县| 唐山市| 桂东县| 双峰县|