android wcf 上传文件,第二篇 ( wcf 与 android 图片上传下载)
老规矩废话不多说,直接入主题
注:wcf 使用rest风格,传递json数据,图片是经过base64编码,android 使用common-codec-1.5.jar 进行base64编码
服务器端
wcf接口:
namespace Test
{
// 注意: 如果更改此处的接口名称 "IService1",也必须更新 Web.config 中对 "IService1" 的引用。
[ServiceContract]
public interface IService1
{
// 任务: 在此处添加服务操作
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "update_pictrue",BodyStyle=WebMessageBodyStyle.WrappedRequest,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]
string update_pictrue(string name, string content, string type);
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "down_pictrue", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string down_pictrue(string name);
}
}
接口实现:
namespace Test
{
// 注意: 如果更改此处的类名“Service1”,也必须更新 Web.config 和关联的 .svc 文件中对“Service1”的引用。
public class Service1 : IService1
{
#region IService1 成员
public string update_pictrue(string name,string content, string type)
{
// throw new NotImplementedException();
if (type != ".jpg" && type != ".gif")
{
return "格式不正确";
}
else
{
string imgFilePath = @"F:\Wcf_teach\pictrue_update_down\Test\update_pictrue\" + name;
if (!File.Exists(imgFilePath))
{
try
{
byte[] ms_content = Convert.FromBase64String(content);
FileStream fs = File.Open(imgFilePath, FileMode.OpenOrCreate);
fs.Write(ms_content, 0, ms_content.Length);
fs.Close();
return "上传成功";
}
catch (Exception ex)
{
return "出现异常";
}
}
else
{
return "上传失败";
}
}
}
public string down_pictrue(string name)
{
// throw new NotImplementedException();
string imgFilePath = @"F:\Wcf_teach\pictrue_update_down\Test\update_pictrue\" + name;
if (File.Exists(imgFilePath))
{
System.IO.FileStream fs = new System.IO.FileStream(imgFilePath, System.IO.FileMode.Open);
int i = (int)fs.Length;
byte[] content = new byte[i];
fs.Read(content, 0, i);
string result = Convert.ToBase64String(content);
fs.Close();
return result;
}
else
{
throw new Exception("没有该文件出现异常");
}
}
#endregion
}
}
客户端
public class pictrue_update_down extends Activity {
private Button update_btn;
private Button down_btn;
private ImageView img_img;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findAll();
bind();
}
public void findAll() {
update_btn = (Button) this.findViewById(R.id.update_btn);
down_btn = (Button) this.findViewById(R.id.down_btn);
img_img = (ImageView) this.findViewById(R.id.img_img);
}
public void bind() {
update_btn.setOnClickListener(mylistener);
down_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;
case R.id.down_btn:
Thread th2 = new Thread(new mythread_down());
th2.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_down implements Runnable {
public void run() {
// TODO Auto-generated method stub
HttpClient hc = new DefaultHttpClient();
HttpPost hp = new HttpPost(
"http://192.168.1.229/Test/service1.svc/down_pictrue");
HttpResponse hr = null;
JSONObject jo1 = new JSONObject();
try {
jo1.put("name", "999.jpg");
StringEntity se = new StringEntity(jo1.toString(), HTTP.UTF_8);
se.setContentType("application/json");
hp.setEntity(se);
hr = hc.execute(hp);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String strResp = null;
if (hr.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
try {
strResp = EntityUtils.toString(hr.getEntity());
File f = new File("//sdcard//999.jpg");
if (!f.exists()) {
f.createNewFile();
FileOutputStream fos=new FileOutputStream(f);
byte[] content= Base64.decodeBase64(strResp.getBytes());
fos.write(content);
fos.flush();
Message msg=hd.obtainMessage(123);
msg.obj="//sdcard//999.jpg";
hd.sendMessage(msg);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
Toast.makeText(pictrue_update_down.this, "下载失败",
Toast.LENGTH_LONG).show();
}
}
}
class mythread implements Runnable {
public void run() {
// TODO Auto-generated method stub
HttpClient hc = new DefaultHttpClient();
HttpPost hp = new HttpPost(
"http://192.168.1.229/Test/service1.svc/update_pictrue");
HttpResponse hr;
String path = "//sdcard//999.jpg";
File f = new File(path);
if (f.exists()) {
// System.out.println("successful");
try {
int ig = (int) f.length();
byte[] content = new byte[ig];
FileInputStream fis;
fis = new FileInputStream(f);
fis.read(content, 0, ig);
String jason = new String(Base64.encodeBase64(content));
JSONObject jo1 = new JSONObject();
jo1.put("name", "999.jpg");
jo1.put("content", jason);
jo1.put("type", ".jpg");
StringEntity se = new StringEntity(jo1.toString(),
HTTP.UTF_8);
se.setContentType("application/json");
hp.setEntity(se);
hr = hc.execute(hp);
String strResp = null;
if (hr.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
strResp = EntityUtils.toString(hr.getEntity());
} else {
strResp = "$no_found_date$";
}
Toast.makeText(pictrue_update_down.this, strResp,
Toast.LENGTH_LONG).show();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
hp.abort();
}
}
}
}
}
android wcf 上传文件,第二篇 ( wcf 与 android 图片上传下载)相关推荐
- ssm上传文件获取路径_ssm框架实现图片上传显示并保存地址到数据库(示例代码)...
本案例是通过springmvc+spring+mybatis框架以商品上传为例,实现的图片上传功能,并把图片的地址保存到数据库并在前台显示上传的图片. 本项目是使用maven搭建的项目,首先看下项目结 ...
- weui上传文件完整例子php,weui实现图片上传
您现在的位置是:网站首页>>前端技术>>weui weui实现图片上传 发布时间:2019-04-30 16:37:17作者:wangjian浏览量:1657点赞量:0 在we ...
- android和ios传文件怎么打开方式,安卓怎么给iPhone传文件?文件互传教程
大家使用手机的时候肯定都会用到蓝牙或者互传文件功能,安卓和安卓手机直接传文件非常简单方便,那么安卓怎么给iPhone传文件呢?小编下面就给大家带来安卓和iPhone文件互传教程. 说道文件互传,大家肯 ...
- 介绍一个工具给大家,做网站时,经常要上传文件到外网服务器,但是上传时往往需要很长时间,如果有一个文件对比工具……...
介绍一个工具给大家,做网站时,经常要上传文件到外网服务器,但是上传时往往需要很长时间,如果有一个文件对比工具, 可以对比每次版本有什么文件变化,并单独找出来.这样只需要上传需求的文件就可以达到版本更新 ...
- mac上传文件到七牛云,使用qshell上传文件到七牛云
第一步:下载qshell(其实就是命令行,不是什么直接能打开的文件,也不是exe文件) 地址:https://developer.qiniu.com/kodo/tools/1302/qshell 选择 ...
- LayUI upload上传组件上传文件的两种方式(手动上传、自动上传)
1 手动上传 上传文件分为两步,第一步选择文件,第二步上传文件. HTML代码: <input type='button' id='selectFile' value='选择文件'> &l ...
- uni-app uni-file-picker文件上传实现拍摄从相册选择获取图片上传文档服务器(H5上传-微信小程序上传)
前言 最近在使用uni-app写H5移动端,有一个从手机拍摄从相册选择获取图片上传到文档服务器功能. 查阅uni-app发现关于上传图片,uni-file-picker文件上传,uni.chooseI ...
- java多图片上传插件,Bootstrap中的fileinput 多图片上传及编辑功能
Bootstrap中的fileinput 多图片上传及编辑功能 2019-01-01 编程之家收集整理的这篇文章主要介绍了Bootstrap中的fileinput 多图片上传及编辑功能,编程之家小编觉 ...
- MdEditor-v3中上传照片的前后端对接(图片上传至又拍云云储存)
MdEditor-v3中上传照片的前后端对接(图片上传至又拍云云储存) 使用springboot+MdEditor-v3+又拍云实现markdown的图片上传功能 需要有一定的springboot和v ...
最新文章
- 关于程序员的59条搞笑但却真实无比的编程语录_技术/软件
- c# 第9节 数据类型之引用类型
- IIR+全通滤波器实现相位平衡_matlab仿真
- 设计模式(九)--注册树模式
- 第三次学JAVA再学不好就吃翔(part76)--Collection类
- 我的Docker-CE学习笔记(03)
- ab st语言编程手册_西门子PLC编程SCL和LAD谁才是王者?一起讨论一下
- Jenkins系列之五——通过Publish over SSH插件实现远程部署
- 深度|二代征信:人行数字解读分与征信复议
- Linux服务器系统管理优化,Linux服务器性能管理与优化
- android实例教程_Android共享首选项示例教程
- 2.熟悉LINUX的基本操作
- 基于STM32移植UCGUI图形界面框架(3.9.0源码版本)
- 公用计算机打不开扫雷,win7系统“扫雷”游戏无法打开问题的处理方法
- 安防摄像头移动侦测和遮挡侦测基本原理
- 计算机原理图及接线图讲解,信号继电器的工作原理和作用以及接线图
- 前端面试送命题-JS三座大山
- 电脑win10系统如何开定位服务器,Win10系统定位功能如何打开 Win10系统定位打开方法...
- 哥德巴赫猜想验证-循环
- Matlab 求方程的根
热门文章
- CentOS 7.2 添加磁盘并创建新区
- WIN7 装2010 没反应,不要虚拟光驱,解压出来就可以安装了
- Unity制作随机数字抽奖小案例
- 打印机无法确认设备和计算机之间的连接,打印机和电脑连接不上怎么办_电脑怎么连接不上打印机设备-win7之家...
- 迅为IMX6ULL开发板Linux学习教程
- 使用奥维地图加载星图地球数据云地图数据
- 管理学中常用的激励理论
- 用事实说话,成熟的ORM性能不是瓶颈,灵活性不是问题:EF5.0、PDF.NET5.0、Dapper...
- html5 驾考 答题样式,考驾照答题软件
- 旗舰版win7系统电脑administrator密码忘记了破解