项目上说是要用到web Service 给其他系统提供接口。然后通过查找资料以及自己的理解和整理 发到这里供大家参考。
1.我先是在项目中写了2个类 然后用MyEclipse自带的web service来生成。
2.对已生成好的web service服务端进行发布后 写了测试方法 测试通过。
3.在web service生成过程中参考http://jyangzi5.iteye.com/blog/671479中的资料,这里的生成过程已经写的很详细,但是我是第一次使用web service,对其中的一些内容理解的不是很透彻,通过自己的理解及测试 整理以下内容:
首先是我写的一个文件上传的类:
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.adobe.internal.pdftoolkit.services.javascript.stubs.auto.Directory;
public class FileUpload {
//文件上传类(该方法用于生成webservice)
public boolean SaveFile(byte[] binData, String fileName) {//传入的参数为:文件字节流,上传后的文件路径及名称
boolean success = false;
File file = new File(fileName);
file.getParentFile().mkdirs();
try {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
new FileOutputStream(file));
bufferedOutputStream.write(binData);
bufferedOutputStream.close();
success = true;
} catch (IOException e) {
e.printStackTrace();
}
return success;
}
// 生成web service时使用的测试方法
public String getUserName(String str) {
return "Hello" + str + ",this is an Axis Dynamic Proxy Web Service";
}
/**
* @param args
*/
public static void main(String[] args) {
FileUpload fu = new FileUpload();
//在main方法中写的在本地进行上传的测试方法
File fileS = new File("f:\\sxm\\nod32激活码.txt");
if (!fileS.exists()) {
System.out.println("找不到指定文件");
return;
}
FileInputStream fileIS = null;
byte[] binData = null;
try {
fileIS = new FileInputStream(fileS);
binData = new byte[(int) fileS.length()];
BufferedInputStream bufferedInputStream = new BufferedInputStream(
new FileInputStream(fileS));
int r = bufferedInputStream.read(binData);
if (r != fileS.length())
throw new IOException("读取文件不正确");
bufferedInputStream.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (fileIS != null) {
try {
fileIS.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
boolean f = fu.SaveFile(binData, "c:\\11.txt");
}
}
其次:类写好了以后 就开始自动生成
步骤为:
1、在新建向导中选择Web Service-->Web Service,在显示的窗口中选择要生成的类:
2、点击下一步,选择要生成web service的方法:
3、点击下一步并完成web service的生成。
现在web service已经生成好了
系统会在项目的WebContent目录下自动生成wsdl文件以及Service、web.xml、wsdd文件
web.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>
org.apache.axis.transport.http.AxisServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
wsdl文件:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://Tools.**.com" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://Tools.**.com" xmlns:intf="http://Tools.**.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
<wsdl:types>
<schema elementFormDefault="qualified" targetNamespace="http://Tools.**.com" xmlns="http://www.w3.org/2001/XMLSchema">
<element name="SaveFile">
<complexType>
<sequence>
<element name="binData" type="xsd:base64Binary"/>
<element name="fileName" type="xsd:string"/>
</sequence>
</complexType>
</element>
<element name="SaveFileResponse">
<complexType>
<sequence>
<element name="SaveFileReturn" type="xsd:boolean"/>
</sequence>
</complexType>
</element>
</schema>
</wsdl:types>
<wsdl:message name="SaveFileRequest">
<wsdl:part element="impl:SaveFile" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="SaveFileResponse">
<wsdl:part element="impl:SaveFileResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="FileUpload">
<wsdl:operation name="SaveFile">
<wsdl:input message="impl:SaveFileRequest" name="SaveFileRequest">
</wsdl:input>
<wsdl:output message="impl:SaveFileResponse" name="SaveFileResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="FileUploadSoapBinding" type="impl:FileUpload">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="SaveFile">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="SaveFileRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="SaveFileResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="FileUploadService">
<wsdl:port binding="impl:FileUploadSoapBinding" name="FileUpload">
// 测试时访问的地址. wsdl的访问地址为http://localhost:8080/Indicator/services/FileUpload?wsdl
<wsdlsoap:address location="http://localhost:8080/Indicator/services/FileUpload"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
wsdd文件中关键部分(我认为这一部分是最主要的部分,如果类中有多个方法的话 只需要在这个wsdd的配置文件中添加多个service即可):
<ns1:service name="FileUpload" provider="java:RPC" style="wrapped" use="literal">//类名
<ns2:operation name="SaveFile" qname="ns1:SaveFile" returnQName="ns1:SaveFileReturn" returnType="xsd:string" soapAction="" xmlns:ns1="http://Tools.xjgzinfo.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns2="http://xml.apache.org/axis/wsdd/">//方法名
<ns2:parameter qname="ns1:binData" type="xsd:byte[]"/>//此处为方法中传入的参数
<ns2:parameter qname="ns1:fileName" type="xsd:String"/>
</ns2:operation>
<ns1:parameter name="allowedMethods" value="SaveFile"/>//方法名
<ns1:parameter name="typeMappingVersion" value="1.2"/>
<ns1:parameter name="wsdlPortType" value="FileUpload"/>
<ns1:parameter name="className" value="com.**.Tools.FileUpload"/>//完整的类名
<ns1:parameter name="wsdlServicePort" value="FileUpload"/>
<ns1:parameter name="schemaQualified" value="http://Tools.**.com"/>
<ns1:parameter name="wsdlTargetNamespace" value="http://Tools.xjgzinfo.com"/>
<ns1:parameter name="wsdlServiceElement" value="FileUploadService"/>
</ns1:service>
然后就是对WebService写的一个测试的方法:
public static void main(String[] args) {
// TODO Auto-generated method stub
String targetEndPoint1 = "http://localhost:8080/Indicator/services/FileUpload";
Service ser = new Service();
try {
Call call = (Call) ser.createCall();
call.setTargetEndpointAddress(targetEndPoint1);
call.setOperation("SaveFile");
File fileS = new File("f:\\sxm\\nod32激活码.txt");
if (!fileS.exists()) {
System.out.println("找不到指定文件");
return;
}
FileInputStream fileIS = null;
byte[] binData = null;
try {
fileIS = new FileInputStream(fileS);
binData = new byte[(int) fileS.length()];
BufferedInputStream bufferedInputStream = new BufferedInputStream(
new FileInputStream(fileS));
int r = bufferedInputStream.read(binData);
if (r != fileS.length())
throw new IOException("读取文件不正确");
bufferedInputStream.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (fileIS != null) {
try {
fileIS.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
boolean flag = (Boolean)call.invoke(new Object[] {binData,new String("c:\\123.txt")});
System.out.println("flag=========================="+flag);
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
web Service 已经生成完毕,启动Tomcat服务
测试结果
flag==========================true
希望以上的内容能大家提供一些参考和帮助。