Android HttpClient上传文件
2015-12-31 11:50
603 查看
参考:http://www.oschina.net/code/piece_full?code=14184&piece=23298#23298
http://www.oschina.net/code/snippet_580112_14184/
http://www.oschina.net/code/snippet_580112_14184/
public String post(String pathToOurFile,String urlServer) throws ClientProtocolException, IOException, JSONException { HttpClient httpclient = new DefaultHttpClient(); //设置通信协议版本 httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); //File path= Environment.getExternalStorageDirectory(); //取得SD卡的路径 //String pathToOurFile = path.getPath()+File.separator+"ak.txt"; //uploadfile //String urlServer = "http://192.168.1.88/test/upload.php"; HttpPost httppost = new HttpPost(urlServer); File file = new File(pathToOurFile); MultipartEntity mpEntity = new MultipartEntity(); //文件传输 ContentBody cbFile = new FileBody(file); mpEntity.addPart("userfile", cbFile); // <input type="file" name="userfile" /> 对应的 httppost.setEntity(mpEntity); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); System.out.println(response.getStatusLine());//通信Ok String json=""; String path=""; if (resEntity != null) { //System.out.println(EntityUtils.toString(resEntity,"utf-8")); json=EntityUtils.toString(resEntity,"utf-8"); JSONObject p=null; try{ p=new JSONObject(json); path=(String) p.get("path"); }catch(Exception e){ e.printStackTrace(); } } if (resEntity != null) { resEntity.consumeContent(); } httpclient.getConnectionManager().shutdown(); return path; }
相关文章推荐
- HTTP
- 【cas、tomcat】HTTP Status 500 - javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateExc
- redis性能测试tcp socket and unix domain
- 时间戳引起的网站访问不了的问题(net.ipv4.tcp_timestamps)
- iOS NSNotificationCenter 使用姿势详解 转载地址http://www.jianshu.com/p/a4d519e4e0d5
- rrc和rrm的区别 http://www.mscbsc.com/bbs/thread-176892-1-1.html
- 广告业务HTTP接口的灰度方案
- Loadrunner之HTTP接口测试
- Sahi (2) —— https/SSL配置(102 Tutorial)
- 网络传输数据格式——JSON和XML
- 菜鸟之路-Listview使用缓存加载网络图片错位Bug解决
- httpclient访问接口
- 关于配置weblogic密匙库信息、SSL,启用HTTPS、禁用HTTP的相关配置文档说明
- java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted()Z 的解决
- IP数据包和TCP数据包是什么关系?当发送数据时,是发送的IP包吗?那TCP包什么时候形成?
- HTTP Debugger Pro使用教程
- #pragma mark --监控网络状态
- Network Link Conditioner模拟不同的网络环境。
- 全站HTTPS来了!有何优势、与HTTP有何不同?
- httpd.conf