android WebSerivce 带图片上传测试代码



package test;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kxml2.kdom.Element;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Test {
/**
* @param args
*/
@SuppressWarnings("restriction")
public static void main(String[] args)throws Exception {
//里面地址对应上面图片的namespace
SoapObject request = new SoapObject("http://webservice.wzkj.com/", "addTravelLog");
String uploadBuffer = null;
try{
FileInputStream fis = new FileInputStream("d:\\1.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count = 0;
while((count = fis.read(buffer)) >= 0){
baos.write(buffer, 0, count);
}
uploadBuffer = (new BASE64Encoder()).encode(baos.toByteArray()); //进行Base64编码

fis.close();
}catch(Exception e){
e.printStackTrace();
}
//这个是配置参数
request.addProperty("userId","1");
request.addProperty("logType","1");
request.addProperty("shareId",0);
request.addProperty("content"," 我发表了一批日志");
request.addProperty("location","成都");
request.addProperty("longitude"," 10.254584");
request.addProperty("latitude"," 10.254584");
request.addProperty("title","如何5杀");
request.addProperty("logImg",uploadBuffer);


SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
Element[] header = new Element[1];
header[0] = new Element().createElement("http://webservice.wzkj.com/","hello");
header[0].setAttribute("http://webservice.wzkj.com/","token","e4rllW/nR6gRuoY/iDx3S7NjghAeQLmyCdMJ5pxDj+OnJOF2g4CxjK98SYgdgLI/itiasGlRAXcKhRzL+G8pzN44/jQDDUr/aT/tnqFxFGBaZ3PKhON9pyzIM7Y4EGYEpLmVKrkRzYDN4PMxT4Plbz04vSRxMiMhuexkgni1SX0=");

envelope.headerOut=header;
envelope.bodyOut = request;
envelope.setOutputSoapObject(request);
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = false;
try {
//对应图片上的soap:address
HttpTransportSE androidHttpTransport = new HttpTransportSE("http://127.0.0.1:8080/wzkj/webservice/friends");
//call的第一个参数对应图片上的soapAction=""
androidHttpTransport.call("", envelope);
SoapObject result = (SoapObject)envelope.bodyIn;
//这里我获取第一个数据
System.out.println(result.getProperty(0).toString());
} catch (Exception e) {
e.printStackTrace();
}
}

}



简单例子


String uploadBuffer = null;
try{
FileInputStream fis = new FileInputStream("d:\\1.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count = 0;
while((count = fis.read(buffer)) >= 0){
baos.write(buffer, 0, count);
}
uploadBuffer = (new BASE64Encoder()).encode(baos.toByteArray()); //进行Base64编码

fis.close();
System.out.println(uploadBuffer);

BASE64Decoder decoder = new BASE64Decoder();
byte[] b = decoder.decodeBuffer(uploadBuffer);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//调整异常数据
b[i]+=256;
}
}
//生成jpeg图片
String imgFilePath = "d://222.jpg";//新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();

}catch(Exception e){
e.printStackTrace();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值