android上传图片、视频、文件,服务端使用wcf接收
最近一直在搞android上传图片、视频、文件,服务端使用wcf接收,本文对调试中的遇到的问题进行记录。
首先android上传一些小图片是比较容易的一天下来差不多就能调试出来,但是上传一些大的文件时就出现各种问题,包括wcf默认支持64k的文件,后来大图片可以上传了,但是传视频又有问题,上传的视频打不开,经过努力google最后问题终于解决了。作者kwstu QQ806693619
以下是调试代码:原文链接:http://www.kwstu.com/ArticleView/kwstu_2013327194830639
一、Android:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
package com.kwstu.palmjn;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.InputStreamEntity;import org.apache.http.impl.client.DefaultHttpClient;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class UpladeActivity extends Activity { private Button update_btn; private ImageView img_img; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.uploadactivity); findAll(); bind(); } public void findAll() { update_btn = (Button) this.findViewById(R.id.update_btn); img_img = (ImageView) this.findViewById(R.id.img_img); } public void bind() { update_btn.setOnClickListener(mylistener); } private View.OnClickListener mylistener = new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.update_btn: Thread th1 = new Thread(new mythread()); th1.start(); break; default: break; } } }; Handler hd = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub // super.handleMessage(msg); if (msg.what == 123) { String jason = msg.obj.toString(); String filepath = Environment.getExternalStorageDirectory() + File.separator + jason; Bitmap bitmap1 = BitmapFactory.decodeFile(filepath); img_img.setImageBitmap(bitmap1); } } }; class mythread implements Runnable { public void run() { // TODO Auto-generated method stub HttpClient hc = new DefaultHttpClient(); HttpPost hp = new HttpPost(wcf地址); HttpResponse hr; InputStreamEntity reqEntity; String path = 上传文件路径; File f = new File(path); if (f.exists()) { System.out.println("successful"); try { reqEntity = new InputStreamEntity(new FileInputStream(path), -1); reqEntity.setContentType("binary/octet-stream"); reqEntity.setChunked(true); // Send in multiple parts if needed hp.setEntity(reqEntity); System.out.println("4"); HttpResponse response = hc.execute(hp); System.out.println("5"); } catch (ClientProtocolException e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); //e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block //e.printStackTrace(); System.out.println(e.getMessage()); } } } }} |
Wcf代码:
接口
|
1
2
3
|
[OperationContract][WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]string update_pictrue(Stream getStream); |
实现
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
public string update_pictrue(Stream getStream) { Console.WriteLine("setData service has bean started!"); string uploadFolder = @"C:\kkk\"; string savaPath = "sss"; //string dateString = DateTime.Now.ToShortDateString() + @"\"; string fileName ="ddd.mp4"; //Stream sourceStream = request.FileData; FileStream targetStream = null; if (!getStream.CanRead) { throw new Exception("数据流不可读!"); } if (savaPath == null) savaPath = @"Photo\"; if (!savaPath.EndsWith("\\")) savaPath += "\\"; uploadFolder = uploadFolder + savaPath; //+ dateString; if (!Directory.Exists(uploadFolder)) { Directory.CreateDirectory(uploadFolder); } string filePath = Path.Combine(uploadFolder, fileName); using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { //read from the input stream in 4K chunks //and save to output stream const int bufferLen = 4096; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = getStream.Read(buffer, 0, bufferLen)) > 0) { targetStream.Write(buffer, 0, count); } targetStream.Close(); getStream.Close(); } return ""; } |
配置很重要必须支持大文件上传
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
<?xml version="1.0"?><configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="Host.ServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> <dataContractSerializer maxItemsInObjectGraph="2147483647"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="json"> <enableWebScript /> </behavior> </endpointBehaviors> </behaviors> <services> <service name="Host.Service" behaviorConfiguration="Host.ServiceBehavior"> <endpoint binding="webHttpBinding" contract="Host.IService" behaviorConfiguration="json" bindingConfiguration="LargeBuffer"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> <host> <baseAddresses> </baseAddresses> </host> </service> </services> <bindings> <webHttpBinding> <binding name="LargeBuffer" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647"/> <security mode="None"></security> </binding> </webHttpBinding> </bindings> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration> |
android上传图片、视频、文件,服务端使用wcf接收的更多相关文章
- 利用webuploader插件上传图片文件,完整前端示例demo,服务端使用SpringMVC接收
利用WebUploader插件上传图片文件完整前端示例demo,服务端使用SpringMVC接收 Webuploader简介 WebUploader是由Baidu WebFE(FEX)团队开发的一 ...
- 服务端增加WCF服务全局异常处理机制
服务端增加WCF服务全局异常处理机制,任一WCF服务或接口方式出现异常,将统一调用WCF_ExceptionHandler.ProvideFault方法,因此不需要每个方法使用try catch写法. ...
- 动态的调用服务端的WCF中的方法
客户端调用wcf ,有时需要动态的调用服务端的WCF中的方法,本方法,反射wcf 的接口,动态调用接口中的方法. 主要为,动态绑定,反射动态调用. public static object Execu ...
- 服务端:WCF服务层安全检查核心类
using System.Data; using CSFrameworkV4_5.Common; using CSFrameworkV4_5.Core.SystemSecurity; using CS ...
- PHP-Socket服务端客户端发送接收通信实例详解
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://fighter.blog.51cto.com/1318618/1533957 So ...
- android使用wcf接收上传图片视频文件
一.Android 权限配置文件: <?xml version="1.0" encoding="utf-8"?> <manifest xmln ...
- Android中直播视频技术探究之---视频直播服务端环境搭建(Nginx+RTMP)
一.前言 前面介绍了Android中视频直播中的一个重要类ByteBuffer,不了解的同学可以 点击查看 到这里开始,我们开始动手开发了,因为我们后续肯定是需要直播视频功能,然后把视频推流到服务端, ...
- android手机推送视频到服务端
项目需求,android手机向服务器推送视频.苦战几个星期终于实现,现记录下来以免以后忘记. 没做过Java,也没做过Android开发,只能现学现卖.在网上找了下搭建开发a ndroid环境资料, ...
- Android 手机卫士--构建服务端json、请求网络数据
本文地址:http://www.cnblogs.com/wuyudong/p/5900384.html,转载请注明源地址. 数据的传递 客户端:发送http请求 http://www.oxx.com/ ...
随机推荐
- 要立刷金组flag了T_T
刷了那么多银组,发现自己好多不会啊... 果然太弱 在这感谢hzwer神犇的blog.. 大部分题解都从黄学长这里来orz. orz.... 果然我太水
- yii2 输出json的方法
public function actionAjax() { if(isset(Yii::$app->request->post('test'))){ $test = "Ajax ...
- hive经常使用命令
hive经常使用命令 show tables; 列出hive里面全部数据表名 desc userProfile; 显示数据表userProfile的基本表字段及字段type desc extended ...
- powershell---高级函数的介绍
https://guhuajun.wordpress.com/2009/05/11/windows-powershell-v2-介绍(5)-高级函数(上)/ https://guhuajun.word ...
- C#中Uri类的解释
URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源.而URL是uniform resource locator,统一资源定位器,它是一种具体的UR ...
- 终端利用ssh登录远程服务器
第一步: 安装ssh:yum install ssh 第二步: 启动ssh服务:service sshd start 第三步: 连接远程服务器: ssh -p 端口号 用户名@ip地址 然 ...
- iOS开发之--当遇到tableView整体上移时的解决方案
方案一在使用了navigationController后,当界面进行跳转往返后,时而会出现tableView上移的情况,通常会自动上移64个像素,那么这种情况,我们可以关闭tableView的自动适配 ...
- Zabbix-3.0.x使用OneAlert发送告警
导读 OneAlert 是国内首个 SaaS 模式的云告警平台,集成国内外主流监控/支撑系统,实现一个平台上集中处理所有 IT 事件,提升 IT 可靠性.它能以史上第二快的速度,对事件进行智能的组织. ...
- poj_2823 单调队列
题目大意 给定一行数,共N个.有一个长度为K的窗口从左向右滑动,窗口中始终有K个数字,窗口每次滑动一个数字.求各个时刻窗口中的最大值和最小值. 题目分析 直接搜索,复杂度为O(n^2).考虑使用单调队 ...
- oracle导入TXT文件
oracle导入TXT文件: 1.建好对应的表和字段:2.新建test.ctl文件,用记事本编辑写入: OPTIONS (skip) load data INFILE 'E:\8080.txt' -- ...