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 阅读(177) 评论(0)  编辑  收藏 所属分类: Android

只有注册用户登录后才能发表评论。


网站导航:
 
<2014年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

Java

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜