android上传文件 例子如下:
1Filefile= newFile(filePath);
2 if(file.exists()){
3StringEND="\r\n";
4StringPREFIX="--";
5StringBOUNDARY=UUID.randomUUID().toString(); // 边界标识随机生成
6 StringCONTENT_TYPE="multipart/form-data"; // 内容类型
7
8InputStreaminputStream= null;
9FileInputStreamfileStream= null;
10DataOutputStreamdos= null;
11HttpURLConnectionconn= null;
12 try{
13URLurl= newURL(Constant.URL_SECONDHAN_TAKEPHOTOATHAND);
14conn=(HttpURLConnection)url.openConnection();
15 /* 设置时间 */
16conn.setReadTimeout(10*1000);
17conn.setConnectTimeout(10*1000);
18
19 /* 允许Input,output,不使用Cache */
20conn.setDoInput( true);
21conn.setDoOutput( true);
22conn.setUseCaches( false);
23
24 /* 设置传送的method=POST */
25conn.setRequestMethod("POST");
26conn.setRequestProperty("Connection","Keep-Alive");
27conn.setRequestProperty("Charset","UTF-8");
28conn.setRequestProperty("Content-Type",CONTENT_TYPE+";boundary="+BOUNDARY);
29
30 /* 设置DataOutputStream */
31dos= newDataOutputStream(conn.getOutputStream());
32
33StringBuffersb= newStringBuffer();
34sb.append(PREFIX);
35sb.append(BOUNDARY);
36sb.append(END);
37 /**
38 *这里重点注意:
39 *name里面的值为服务器端需要key只有这个key才可以得到对应的文件
40 *filename是文件的名字,包含后缀名的比如:abc.png
41 */
42sb.append("Content-Disposition:form-data;name=\"UpLoadFile\";filename=\""+file.getName()+"\""+END);
43sb.append("Content-Type:application/x-zip-compressed;charset=utf-8"+END);
44sb.append(END);
45dos.write(sb.toString().getBytes());
46
47 /* 取得文件的FileInputStream */
48fileStream= newFileInputStream(file);
49
50 /* 设置每次写入1024bytes */
51 intbufferSize=1024;
52 byte[]buffer= new byte[bufferSize];
53 intlength=-1;
54
55 /* 从文件读取数据至缓冲区 */
56 while((length=fileStream.read(buffer))!=-1){
57dos.write(buffer,0,length);
58}
59dos.write(END.getBytes());
60 byte[]end_data=(PREFIX+BOUNDARY+PREFIX+END).getBytes();
61dos.write(end_data);
62dos.flush();
63
64 /* 取得Response内容 */
65InputStreamis=conn.getInputStream();
66 intch;
67StringBufferb= newStringBuffer();
68 while((ch=is.read())!=-1){
69b.append(( char)ch);
70}
71Stringresult=b.toString();
72
73} catch(SocketTimeoutExceptione){
74AgUtils.log("","uploadSocketTimeoutException");
75mHandler.sendEmptyMessage(4);
76e.printStackTrace();
77} catch(ConnectTimeoutExceptione){
78AgUtils.log("","downloadConnectTimeoutException");
79mHandler.sendEmptyMessage(4);
80e.printStackTrace();
81} catch(UnknownHostExceptione){
82AgUtils.log("","downloadUnknownHostException");
83mHandler.sendEmptyMessage(4);
84e.printStackTrace();
85} catch(MalformedURLExceptione){
86AgUtils.log("","downloadMalformedURLException");
87mHandler.sendEmptyMessage(4);
88e.printStackTrace();
89} catch(ProtocolExceptione){
90AgUtils.log("","ProtocolException");
91e.printStackTrace();
92mHandler.sendEmptyMessage(2);
93} catch(IOExceptione){
94AgUtils.log("","IOException");
95mHandler.sendEmptyMessage(0);
96e.printStackTrace();
97} finally{
98XqshService. this.sendBroadcast( newIntent(INTENT_UPLOAD_COMPLETE));
99 try{
100 if(fileStream!= null){
101fileStream.close();
102}
103 if(inputStream!= null){
104inputStream.close();
105}
106 if(dos!= null){
107dos.close();
108}
109
110 if(conn!= null){
111conn.disconnect();
112conn= null;
113}
114} catch(IOExceptione){
115e.printStackTrace();
116}
117}
118}

更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. 读取android手机流量信息
  4. android EditText设置不可写
  5. android 使用html5作布局文件: webview跟javascript交互
  6. Android(安卓)多媒体扫描过程(Android(安卓)Media Scanner Proces
  7. android“设置”里的版本号
  8. Android开发环境搭建
  9. Android(安卓)Resource介绍和使用

随机推荐

  1. Android(安卓)面试题总结之Android(安卓)
  2. Ubuntu下android学习——(2)Android系统构
  3. [Android算法] Android蓝牙开发浅谈
  4. Android(安卓)设置页面的设计
  5. Android工程 引用另外一个Android工程(类
  6. Android官方离线文档(API文档)打开速度慢的
  7. Android中Cursor关闭的问题
  8. Android应用程序资源管理器(Asset Manager
  9. Android:增强目录选择器对话框
  10. Android(安卓)- 支持不同的设备