//安卓部分的代码public class UploadFile { public static void toUploadFile(String url, String path) throws FileNotFoundException { try { // 1,读出文件到byte[] InputStream is = new FileInputStream(path); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; // buff用于存放循环读取的临时数据 int rc = 0; while ((rc = is.read(buff, 0, 1024)) > 0) { os.write(buff, 0, rc); } ByteArrayEntity reqEntity = new ByteArrayEntity(os.toByteArray()); reqEntity.setContentType("binary/octet-stream"); reqEntity.setChunked(true); // 2, 初始化Post, 添加参数 HttpPost httpPost = new HttpPost(url); httpPost.setHeader("charset", HTTP.UTF_8); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-Type", "binary/octet-stream"); httpPost.setEntity(reqEntity); DefaultHttpClient client = getHttpClient(); HttpResponse response = client.execute(httpPost);// 上传文件 int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { System.out.println("调用成功: statusCode : " + statusCode); return; } else { System.out.println("调用失败:code=" + statusCode); return; } } catch (ClientProtocolException e) { System.out.println(e.toString()); } catch (IOException e) { System.out.println(e.toString()); } catch (Exception e) { System.out.println(e.toString()); } } protected static DefaultHttpClient getHttpClient() { DefaultHttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 300);// 连接时间 client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3600 * 1000);// 超时时间 return client; }}//WCF部分的代码[WebInvoke(UriTemplate = "ReceiveImg2", Method = "POST")]public string ReceiveImg2(Stream request){ try { string path = System.Web.Hosting.HostingEnvironment.MapPath("~") + "1.jpg"; using (var file = File.Create(path)) { request.CopyTo(file); } return "1"; } catch { return "-1"; }}web.Config里面 basicHttpBinding 的配置<basicHttpBinding> <binding name="DocumentExplorerServiceBinding" sendTimeout="00:10:00" transferMode="Streamed" messageEncoding="Text" textEncoding="utf-8" maxReceivedMessageSize="9223372036854775807"> </binding></basicHttpBinding>
详细解决方案
安卓使用WCF的Restful形式上传文件
热度:83 发布时间:2016-04-28 01:51:11.0
相关解决方案
- Restful(2)Spring整合CXF,发布RSETful 风格WebService
- JSR 311(JAX-RS: Java API for RESTful Web Services) 轨范正式发布
- 应用 WCF 实现 RESTful 方式的 WebService
- web.py 0.3 生手指南 - RESTful doctesting using app.request
- RESTful Web Service(3):使用ajax创建客户端
- RESTful Web 服务 Jersey运用
- JSR 311终极草案:RESTful Web服务Java API
- Struts、Ajax、Restful、Web Service 狂想曲框架兑现篇(二)
- 应用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- 应用 Spring 3 来创建 RESTful Web Services
- springmvc restful 中文参数有关问题
- 如何写jax-RS/Jersey/restful webservice里头的方法
- springMVC RESTful,如何判断权限
- js 调用 RESTful WCF Services 疏失
- 七学习分布式架构-Redis使用场景 zookeeper FastDFS maven springmvc mybatis shiro restful
- 一学习分布式架构-介绍(Dubbo zookeeper FastDFS JMS maven springmvc mybatis shiro restful )
- 六学习分布式架构-proxy代理 zookeeper FastDFS maven springmvc mybatis shiro restful
- 七、RESTFul
- RESTful API接口设计标准及规范总结——张人杰
- Tp5 如何使用 restful api
- 简略整理一下RPC、WebService、Restful、Json、Xml这些概念之间的关系
- restful api 自我见解的一个分页小插件
- RestFul spring 的GET和POST接口编写
- 使用ASP.NET Core 3.x 构建 RESTful API - 3.4 内容协商