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();
}