java调用.net webservice 返回图片

由于没学习过.net,最近程序中需要调用.net的webservice接口得到一张图片。在网上找了好多资料选用的axis来实现调用。
首先看看.net实现Webmehod的两种方式,这两种方式java传参数的方式也有不同
1.rpc方式:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace WebService1
{

[WebService(Namespace = "http://tempuri.org/")]
public class Service1 : System.Web.Services.WebService
{

[WebMethod]
[SoapRpcMethod(Action = "http://tempuri.org/hello", RequestNamespace = "http://tempuri.org/", ResponseNamespace = "http://tempuri.org/")]

public string hello(String s)
{
return "Hello,"+s;
}
}
}


2.Document方式:

namespace WebService1
{

[WebService(Namespace = "http://tempuri.org/")]
public class Service1 : System.Web.Services.WebService
{

[WebMethod]
public string hello(String s)
{
return "Hello,"+s;
}
}
}




下面是java调用的代码

package com.ctitc.analysis.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Date;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import sun.misc.BASE64Decoder;

import com.ctitc.analysis.pojo.GisChartInfo;


/*
* 调用.net的webservice方法获取gis生成的图片地址
*
* */
public class GetWebService {

public String getWebService(String imagePath,GisChartInfo gisChartInfo) {
String picUrl = "";
try {
// WebService URL
String url = "http://192.168.5.92/JttMapService/Service.asmx";
String namespace = "http://www.fzkcy.com/";
String method = "WebChartNew";

//创建调用服务
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
/*如果.net在提供Rpc服务时设置成
[code="java"]
[SoapRpcMethod(Action = "",.....
则不需要再设置call.setUseSOAPAction和call.setSOAPActionURI*/


call.setUseSOAPAction(true);
call.setSOAPActionURI("http://www.fzkcy.com/WebChartNew");

// 设置要调用的方法
call.setOperationName(new QName(namespace, method));

// 该方法需要的参数

call.addParameter(new QName(namespace,"layername"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName(namespace,"width"),XMLType.XSD_INT,ParameterMode.IN);
call.addParameter(new QName(namespace,"height"),XMLType.XSD_INT,ParameterMode.IN);
call.addParameter(new QName(namespace,"imageType"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName(namespace,"fieldnames"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName(namespace,"fieldvalues"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName(namespace,"rgbcolors"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName(namespace,"chartType"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName(namespace,"size"),XMLType.XSD_STRING,ParameterMode.IN);
//以下传参数方式为 Rpc方式
// call.addParameter("layername",XMLType.XSD_STRING,ParameterMode.IN);
// call.addParameter("width",XMLType.XSD_INT,ParameterMode.IN);
// call.addParameter("height",XMLType.XSD_INT,ParameterMode.IN);
// call.addParameter("imageType",XMLType.XSD_STRING,ParameterMode.IN);
// call.addParameter("fieldnames",XMLType.XSD_STRING,ParameterMode.IN);
// call.addParameter("fieldvalues",XMLType.XSD_STRING,ParameterMode.IN);
// call.addParameter("rgbcolors",XMLType.XSD_STRING,ParameterMode.IN);
// call.addParameter("chartType",XMLType.XSD_STRING,ParameterMode.IN);
// call.addParameter("size",XMLType.XSD_STRING,ParameterMode.IN);
// 方法的返回值类型
call.setReturnType(XMLType.XSD_STRING);

//设置参数
String layername = gisChartInfo.getLayername();
int width = gisChartInfo.getWidth();
int height = gisChartInfo.getHeight();
String imageType = gisChartInfo.getImageType();
String fieldnames = gisChartInfo.getFieldnames();
String fieldvalues = gisChartInfo.getFieldvalues();
String rgbcolors = gisChartInfo.getRgbcolors();
String chartType = gisChartInfo.getChartType();
String size = gisChartInfo.getSize();

Object[] parameters = new Object[]{layername,width+"",height+"",imageType,fieldnames,fieldvalues,rgbcolors,chartType,size};
// 调用该方法
byte[] imageBytes ;
BASE64Decoder dec=new BASE64Decoder();
imageBytes = dec.decodeBuffer(call.invoke(parameters).toString());
System.out.println(imageBytes);
//在指定位置生成图片
Date date = new Date();
picUrl = imagePath+date.getTime()+"."+gisChartInfo.getImageType();
FileOutputStream out = new FileOutputStream(picUrl);
System.out.println(imageBytes.length);
out.write(imageBytes ,0,imageBytes.length);
out.flush();
out.close();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return picUrl;
}

public static void main(String args[]){
GisChartInfo gisChartInfo = new GisChartInfo();
gisChartInfo.setLayername("地市行政区划");
gisChartInfo.setWidth(800);
gisChartInfo.setHeight(800);
gisChartInfo.setImageType("JPG");
gisChartInfo.setFieldnames("行政区划代码,县道,乡道,村道,专用道路");
gisChartInfo.setFieldvalues("3501,1791.561,2327.151,4659.821,9.656;" +
"3502,493.664,665.151,405.498,0;" +
"3503,676.446,981.887,3266.88,3.095;" +
"3504,1846.326,6357.708,3388.415,0;" +
"3505,1966.458,6469.783,4600.375,1.461;" +
"3506,1692.84,3526.773,3656.503,0;" +
"3507,1541.45,6636.189,3572.128,99.667;" +
"3508,1797.244,6723.257,1750.648,296.853;" +
"3509,1725.352,1903.754,4550.091,58.943");
gisChartInfo.setRgbcolors("0,92,230;132,0,128;56,168,0;255,128,0");
gisChartInfo.setChartType("BAR");
gisChartInfo.setSize("30,10");
System.out.println(new GetWebService().getWebService("d:/GisPictures/", gisChartInfo));

}


}

[/code]

下面是接口描述文档

POST /JttMapService/Service.asmx HTTP/1.1
Host: 192.168.5.92
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.fzkcy.com/WebChartNew"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<WebChartNew xmlns="http://www.fzkcy.com/">
<layername>string</layername>
<width>int</width>
<height>int</height>
<imageType>string</imageType>
<fieldnames>string</fieldnames>
<fieldvalues>string</fieldvalues>
<rgbcolors>string</rgbcolors>
<chartType>string</chartType>
<size>string</size>
</WebChartNew>
</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<WebChartNewResponse xmlns="http://www.fzkcy.com/">
<WebChartNewResult>base64Binary</WebChartNewResult>
</WebChartNewResponse>
</soap:Body>
</soap:Envelope>




其中需要注意的是java在接受.net的webservice方法返回的是经过base64编码方式编码过的字符串,接收时需要将它转换为byte[]再生成图片。
还有一点我没搞明白的,也困扰我很久的就是.net的接口描述文档中的参数width和height是int类型,当我传入int类型时服务器会报错:Server was unable to read request。就是服务器无法读取请求。最后通过把这两个值转换为String类型传入解决问题,但原因还没搞清楚,希望那位是过来人的仁兄留言解释一下,不胜感激!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值