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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章