Android文件上传
2014-02-20 17:31
337 查看
/** * 文件上传 * * @param file * 上传的文件 * @param urlStr * 上传到的地址 * @return 上传完成后的结果 * @throws Exception * 异常 */ public String httpClientUpload(File file, String urlStr) throws Exception { String result = ""; HttpPost post = null; HttpResponse response = null; InputStreamEntity reqEntity = null; try { if (CommonUtils.networkIsAvaiable(mContext)) { post = new HttpPost(urlStr); if (null != HttpHandler.mCookieStore) { SingletonHttpClient.getHttpClient().setCookieStore( HttpHandler.mCookieStore); LogUtils.printLog(TAG, "httpClientUpload:setCookie", HttpHandler.mCookieStore.toString()); } reqEntity = new InputStreamEntity(new FileInputStream(file), -1); reqEntity.setContentType("binary/octet-stream"); reqEntity.setChunked(true); post.setEntity(reqEntity); response = SingletonHttpClient.getHttpClient().execute(post); if (HttpURLConnection.HTTP_OK == response.getStatusLine() .getStatusCode()) { HttpHandler.mCookieStore = SingletonHttpClient .getHttpClient().getCookieStore(); result = EntityUtils.toString(response.getEntity(), HttpHandler.CHARSET); LogUtils.printLog(TAG, "httpClientUpload:getCookie", HttpHandler.mCookieStore.toString()); } } return result; } finally { file = null; urlStr = null; result = null; if (post != null && !post.isAborted()) { post.abort(); } response = null; reqEntity = null; } }
相关文章推荐
- android下xml序列化工具,生成xml文件方法
- Android 2.1下调试3G模块
- listview android:cacheColorHint,android:listSelector属性作用
- Android动画学习笔记-Android Animation(转)
- android Loader eoe_blog_933560_author
- android项目各个文件详解
- 【Android Developers Training】 68. 序言:添加动画
- Android ContentProvider 内容提供者
- Android开发规范详解
- 限制Android的EditText输入文字时换行
- phoneGap-Android开发环境搭建
- android 获取SD、ROM容量
- Android从SD卡读取图片并显示
- Android TextView 支持的HTML标签
- Android Sip学习(二)Android VoIP系统实现原理
- android中soap协议使用(ksoap调用webservice)
- Android手机加速度传感器和方向传感器的应用
- android进阶(三)数据存储之Internal Storage
- Android 系统信息获取(CPU,RAM,ROM,Battery,SD-card,版本等)
- Android ADB server didn't ACK * failed to start daemon * 简单有效的解决方案