android之调用webservice 实现图片上传
来源:互联网 发布:chemistwarehouse知乎 编辑:程序博客网 时间:2024/06/05 15:17
http://www.cnblogs.com/top5/archive/2012/02/16/2354517.html
public void testUpload(){ try{ String srcUrl = "/sdcard/"; //路径 String fileName = "aa.jpg"; //文件名 FileInputStream fis = new FileInputStream(srcUrl + fileName); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int count = 0; while((count = fis.read(buffer)) >= 0){ baos.write(buffer, 0, count); } String uploadBuffer = new String(Base64.encode(baos.toByteArray())); //进行Base64编码 String methodName = "uploadImage"; connectWebService(methodName,fileName, uploadBuffer); //调用webservice Log.i("connectWebService", "start"); fis.close(); }catch(Exception e){ e.printStackTrace(); } } connectWebService()方法://使用 ksoap2 调用webservice private boolean connectWebService(String methodName,String fileName, String imageBuffer) { String namespace = "http://134.192.44.105:8080/SSH2/service/IService"; // 命名空间,即服务器端得接口,注:后缀没加 .wsdl, //服务器端我是用x-fire实现webservice接口的 String url = "http://134.192.44.105:8080/SSH2/service/IService"; //对应的url //以下就是 调用过程了,不明白的话 请看相关webservice文档 SoapObject soapObject = new SoapObject(namespace, methodName); soapObject.addProperty("filename", fileName); //参数1 图片名 soapObject.addProperty("image", imageBuffer); //参数2 图片字符串 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER10); envelope.dotNet = false; envelope.setOutputSoapObject(soapObject); HttpTransportSE httpTranstation = new HttpTransportSE(url); try { httpTranstation.call(namespace, envelope); Object result = envelope.getResponse(); Log.i("connectWebService", result.toString()); } catch (Exception e) { e.printStackTrace(); } return false; }
2。 服务器端的webservice代码 :
public String uploadImage(String filename, String image) {
FileOutputStream fos = null;
try{
String toDir = "C:\\Program Files\\Tomcat 6.0\\webapps\\SSH2\\images"; //存储路径
byte[] buffer = new BASE64Decoder().decodeBuffer(image); //对android传过来的图片字符串进行解码
File destDir = new File(toDir);
if(!destDir.exists()) destDir.mkdir();
fos = new FileOutputStream(new File(destDir,filename)); //保存图片
fos.write(buffer);
fos.flush();
fos.close();
return "上传图片成功!" + "图片路径为:" + toDir;
}catch (Exception e){
e.printStackTrace();
}
return "上传图片失败!";
}
- android之调用webservice 实现图片上传
- android之调用webservice 实现图片上传
- android之调用webservice 实现图片上传
- android之调用webservice 实现图片上传
- android之调用webservice 实现图片上传
- android之调用webservice 实现图片上传
- android之调用webservice 实现图片上传
- android之调用webservice 实现图片上传
- android调用webservice实现图片上传
- android调用webservice实现图片上传
- android 调用webservice 图片上传
- android之调用webservice 实现图片上传下载
- Android之调用webservice 实现图片上传下载
- android之调用webservice 实现图片上传下载
- Android之调用webservice 实现图片上传下载
- android之调用webservice 实现图片上传下载
- android之调用webservice 实现图片上传下载
- Android之调用webservice 实现图片上传下载(转)
- USACO / Stringsobits (DP构造/康托展开)
- POJ3094-Quicksum解题报告
- Android操作HTTP实现与服务器通信
- COM读书笔记---- 编程工作的简化
- 分页的极致
- android之调用webservice 实现图片上传
- 产品经理值得看的16个博客
- python转存.seq文件为.jpg序列
- Android Activity去除标题栏和状态栏
- Fedora14 源码安装qt4.7.0 PC版 和 arm版 过程 及问题解决
- Unity纯新手包会入门教程(四)
- 关机程序代码
- 软件设计模式之单例模式
- Unity纯新手包会入门教程(五)