一服务器端C#
这里有三个上传方法
1.uploadFile( byte []bs, String fileName);
PC机操作是没有问题
2. uploadImage(String filename,String image);
//android大于1M上传会出问题(内存溢出),把文件件转换为Base64字符串上传
3. uploadResume(String filename,
String image, int tag); //android可以传大文件

using System;
using
System.Collections.Generic;
using System.Web;
using
System.Web.Services;
using System.IO;
///
/// Summary description for
service1
///
[WebService(Namespace = "http://myserver1.cn/")]
[WebServiceBinding(ConformsTo =
WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from
script, using ASP.NET AJAX, uncomment the following line.
//
[System.Web.Script.Services.ScriptService]
public class service1 :
System.Web.Services.WebService {
public service1 () {
//Uncomment the
following line if using designed
components
//InitializeComponent();
}
[WebMethod]
public string
HelloWorld(string str1) {
return str1+"成功";
}
[WebMethod]
public int
Add(int a, int b)
{
return a + b;
}
[WebMethod]
public int
uploadFile( byte []bs, String fileName){

FileStream out1 =null;
try
{
String
path=String.Format("{0:yyyyMMdd_hhmmss}_{1}",DateTime.Now,fileName);
String
newFile = HttpContext.Current.Server.MapPath("upload/"+path); //
上传文件存放路径
out1 = new FileStream(newFile, FileMode.CreateNew,
FileAccess.Write);
try {
out1.Write(bs,0,bs.Length);
} catch
(IOException e) {
// TODO Auto-generated catch block
;
}
} catch
(FileNotFoundException e) {
// TODO Auto-generated catch block

return
- 1 ;
} finally {
if (out1 != null ) {
try {
out1.Close();
}
catch (IOException e) {
// TODO Auto-generated catch
block

}
}
}
return 0 ;
}
[WebMethod]
//android大于1M上传会出问题(内存溢出)
public int uploadImage(String filename,String
image){

FileStream out1 =null;
byte
[]bs=Convert.FromBase64String(image);
try {
String
path=String.Format("{0:yyyyMMdd_hhmmss}_{1}",DateTime.Now,filename);
String
newFile = HttpContext.Current.Server.MapPath("upload/"+path); //
上传文件存放路径
out1 = new FileStream(newFile, FileMode.CreateNew,
FileAccess.Write);
try {
out1.Write(bs,0,bs.Length);
} catch
(IOException e) {
// TODO Auto-generated catch block
;
}
} catch
(FileNotFoundException e) {
// TODO Auto-generated catch block

return
- 1 ;
} finally {
if (out1 != null ) {
try {
out1.Close();
}
catch (IOException e) {
// TODO Auto-generated catch
block

}
}
}
return 0 ;
}
[WebMethod] //断点续传
public int
uploadResume(String filename, String image, int tag)
{
FileStream out1 =
null;
byte[] bs = Convert.FromBase64String(image);
try
{

String
newFile = HttpContext.Current.Server.MapPath("upload/" + filename); //
上传文件存放路径
if (tag ==
0)
{

if(File.Exists(filename))
File.Delete(filename);
out1 = new
FileStream(newFile, FileMode.CreateNew,
FileAccess.Write);
}
else
{
out1 = new FileStream(newFile,
FileMode.Append, FileAccess.Write);
}
try
{
out1.Write(bs, 0,
bs.Length);
}
catch (IOException e1)
{
// TODO Auto-generated catch
block
;
}
}
catch (FileNotFoundException e2)
{
// TODO
Auto-generated catch block
return -1;
}
finally
{
if (out1 !=
null)
{
try
{
out1.Close();
}
catch (IOException e)
{
//
TODO Auto-generated catch block
}
}
}
return tag;
}
}
二.客户端
要使用 ksoap2库

package cn.serverice;

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.text.SimpleDateFormat;

import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.util.Base64; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView;

public class Webserverice extends Activity {     /** Called when the activity is first created. */  private final String NAMESCROPE="http://mywebservice.cn/";  private final String METHOD_NAME="uploadResume";  private final String URL="http://192.168.1.18/3g/WebService.asmx";  private final String SOAP_ACTION="http://mywebservice.cn/uploadResume";  private Button btn;  private TextView txt1;    Handler handler=null;    //进程中调用view不安全

String str1="";   //返回调用值         @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);                 txt1=(TextView)findViewById(R.id.txt1);                 btn=(Button)findViewById(R.id.btnok);         btn.setOnClickListener(new OnClickListener(){                  @Override   public void onClick(View v) {    // TODO Auto-generated method stub           final String filename="/sdcard/DCIM/012.3gp";                               handler=new Handler();           @SuppressWarnings("unused")     Thread webserviceThread = new Thread(){             @Override      public void run(){               uploadTest(filename);                          }                                   };           webserviceThread.start();   }                      });            }     private void uploadTest(String filename)     {      SimpleDateFormat sDateFormat =new SimpleDateFormat("yyyy-MM-dd_hhmmss");         String file1=sDateFormat.format(new java.util.Date())+filename.substring(filename.indexOf("."));       try     {       FileInputStream fis=new FileInputStream(filename);       //ByteArrayOutputStream baos=new ByteArrayOutputStream();       byte []buffer=new byte[100*1024];       int count=0;       int i=0;       while((count=fis.read(buffer))>=0){        String uploadBuffer=new String(Base64.encode(buffer,0,count,Base64.DEFAULT));           showServerice(uploadBuffer,file1,i);  //续传                     for(int j=0;j<1000;j++);                     i++;        }              fis.close();      }catch(FileNotFoundException e){        e.printStackTrace();      }catch(IOException e){         e.printStackTrace();       }     }

public void showServerice(String image,String file1,int tag)     {      SoapObject request = new SoapObject(NAMESCROPE, METHOD_NAME);      SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);            try {    request.addProperty("filename", file1);    request.addProperty("image",image);    request.addProperty("tag",tag);            envelope.bodyOut=request;     envelope.dotNet=true;    envelope.setOutputSoapObject(request);   } catch (Exception e1) {    // TODO Auto-generated catch block    Log.e("Error","错误1");   }            HttpTransportSE ht=new HttpTransportSE(URL);      ht.debug=true;      try      {       ht.call(SOAP_ACTION, envelope);       SoapObject result = (SoapObject) envelope.bodyIn;                    str1=result.getProperty(0).toString();                         //txt1.setText("uploadImage(filename,image)="+str1+" 成功!"); 在进程中不安全,要加入Runnable             handler.post(runnableUi);             }catch(Exception e){       Log.d("Error",e.getMessage());      }           }     Runnable runnableUi=new  Runnable(){      //给文本框设值          @Override             public void run() {                  //更新界面            txt1.setText("uploadImage(filename,image)="+str1+" 成功!");             }                         };

}

Android调用Webservice发送文件的更多相关文章

  1. 纠正网上乱传的android调用Webservice方法。

    1.写作背景: 笔者想实现android调用webservice,可是网上全是不管对与错乱转载的文章,结果不但不能解决问题,只会让人心烦,所以笔者决定将自己整理好的能用的android调用webser ...

  2. Android调用WebService(转)

    Android调用WebService WebService是一种基于SOAP协议的远程调用标准,通过 webservice可以将不同操作系统平台.不同语言.不同技术整合到一块.在Android SD ...

  3. sqlserver能否调用webservice发送短信呢?

    上班的时候突然有一个想法,sqlserver能否调用webservice发送短信呢? 经过查找资料,终于找到了解决办法,现将步骤贴到下面: (1)开启sqlserver组件功能,如果不开启这个组件功能 ...

  4. Android使用webService(发送xml数据的方式,不使用jar包)

    Android使用webService可以用ksoap2.jar包来使用.但是我觉得代码不好理解,而且记不住. 所以我查询了好多资料,以及自己的理解.可以用代码发送http请求(发送xml数据)来访问 ...

  5. 第十五章:Android 调用WebService(.net平台)

    什么是webservice? Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和 ...

  6. Android 调用webService(.net平台)

    什么是webservice? Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和 ...

  7. 【Android进阶】Android调用WebService的实现

    最近想自己搞搞服务器,就从最简单的webservice开始吧 先上效果图 项目结构 开始贴代码,注释都有,有问题的请留言 MainActivity.java package com.example.w ...

  8. 网摘Android调用WebService

    这边特别注意调用的.net WCF 接口的绑定方式.以前一直用的wxHttpbinding,一直连不上.改成BasicHTTPbinding就能连上了 上篇文章已经对Web Service及其相关知识 ...

  9. android调用webservice接口获取信息

    我的有一篇博客上讲了如何基于CXF搭建webservice,service层的接口会被部署到tomcat上,这一篇我就讲一下如何在安卓中调用这些接口传递参数. 1.在lib中放入ksoap2的jar包 ...

随机推荐

  1. 有可能挑战Java优势的四种技术

    2012-02-22  Java是一种杰出的产业开发语言,这是因为它带来了伟大的统一和对事实上以前并不存在的重要标准的关注.但是和所有语言一样,Java将来也会褪色.依据我做的超越Java的研究,一个 ...

  2. JavaScript Math Object 数字

    JavaScript Math Object Math Object The Math object allows you to perform mathematical tasks. Math is ...

  3. 路由器下CLI界面

    CLI(command-line interface,命令行界面)是指可在用户提示符下键入可执行指令的界面. CLI是Command Line Interface的缩写,即命令行界面.CLI界面是所有 ...

  4. STS 设置代码注释模板

    打开Window->Preferences->Java->Code Style->Code Templates <?xml version="1.0" ...

  5. js 对文件操作

    下面是对此知识的系统介绍(转自互联网): Javascript是网页制作中离不开的脚本语言,依靠它,一个网页的内容才生动活泼.富有朝气.但也许你还没有发现并应用它的一些更高级的功能吧?比如,对文件和文 ...

  6. 免费的UI素材准备

    UI素材准备 UI也是一个专业性比较强的一个活啊,不过还好我有强大的百度,强大的百度有各种强大的网站,下面介绍一些UI常用的网站1.阿里巴巴矢量图标库 http://www.iconfont.cn/p ...

  7. 【Linux】字符转换命令paste

    这个 paste 就要比join 简单多了!相对于 join 必须要比对两个文件的数据相关性, paste 就直接『将两行贴在一起,且中间以 [tab] 键隔开』而已!简单的使用方法: [root@w ...

  8. Android水波纹特效的简单实现

    我的开源页面指示器框架 MagicIndicator,各位一定不要错过哦. 水波纹特效,想必大家或多或少见过,在我的印象中,大致有如下几种: 支付宝 "咻咻咻" 式 流量球 &qu ...

  9. MySQL中 optimize table '表名'的作用

    语法: optimize table '表名' 一,原始数据 1,数据量 2,存放在硬盘中的表文件大小 3,查看一下索引信息 索引信息中的列的信息说明. Table :表的名称.Non_unique: ...

  10. Mysql命令行改动字段类型

    在做微信公众平台 知识百科(账号:zhishiwiki) 时,由于字段先前设计的不合理.导致内容装不下,因此须要改动其字段类型为 text 这里使用到了 alter 命令 alter table 表名 ...