package cn.microvideo.oa.ws.dao;
import javax.jws.WebService;
import cxfTest.Annex;
@WebService
public interface IHelloWorld {
public String sayHello(String name);
public Annex getAnnex();
public String addAnnex(Annex annex) ;
}
接口,也可以在类上面指定服务端的soap协议的版本
package cn.microvideo.oa.ws.dao.impl;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import cxfTest.Annex;
import cn.microvideo.oa.ws.dao.IHelloWorld;
public class HelloWorld implements IHelloWorld {
@Override
public String sayHello(String name) {
System.out.println(name+"sayHello被调用");
return "hello"+name;
}
@Override
public Annex getAnnex() {
System.out.println("getAnnex被调用");
Annex annex =new Annex();
annex.setDataType("不错");
annex.setDocumentName("这是测试问");
annex.setFileType("ces");
annex.setLength(500);
return annex;
}
@Override
public String addAnnex(Annex annex) {
InputStream is=null;
OutputStream os=null;
try {
is = annex.getFileData().getDataSource().getInputStream();
os = new FileOutputStream(new File("d:"+File.separator+annex.getFileData().getName()));
byte[] b = new byte[100000];
int bytesRead = 0;
while ((bytesRead = is.read(b)) != -1) {
os.write(b, 0, bytesRead);
}
os.close();
is.close();
return "success";
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "fail";
}
}
实现类,下载是用了mtom
package cxfTest;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import cn.microvideo.oa.ws.dao.IHelloWorld;
import cn.microvideo.oa.ws.dao.impl.HelloWorld;
public class TestCxf {
public static void main(String[] args) {
JaxWsServerFactoryBean factory=new JaxWsServerFactoryBean();
factory.setAddress("http://localhost:8888/MyCxfService");
factory.setServiceClass(IHelloWorld.class);
factory.setServiceBean(new HelloWorld());
//加入请求消息拦截器
factory.getInInterceptors().add(new LoggingInInterceptor());
//加入响应消息拦截器
factory.getOutInterceptors().add(new LoggingOutInterceptor());
Server server=factory.create();
System.err.println("开始创建~");
//WebClient.create(factory.getAddress()).accept(MediaType.APPLICATION_XML);
server.start();
}
}
j2se发布
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 导入cxf框架的3个配置文件 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 第二种发布方式:带有接口的发布 -->
<jaxws:server id="hiService" address="/hi" serviceClass="cn.microvideo.oa.ws.dao.IHelloWorld">
<jaxws:serviceBean>
<!-- 提供服务的实现类 -->
<bean class="cn.microvideo.oa.ws.dao.impl.HelloWorld"></bean>
</jaxws:serviceBean>
<jaxws:properties>
<entry key="mtom-enabled" value="true"/>
</jaxws:properties>
</jaxws:server>
</beans>
spring发布
package cxfTest;
import java.io.File;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import cn.microvideo.oa.util.JaxbUtils;
import cn.microvideo.oa.ws.dao.IHelloWorld;
import cn.microvideo.oa.ws.dao.impl.HelloWorld;
public class TestCxfClent {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory =new JaxWsProxyFactoryBean();
// factory.setAddress("http://localhost:8888/MyCxfService");
factory.setAddress("http://localhost:8080/xzhd_oa/cxfws/hi");
factory.setServiceClass(IHelloWorld.class);
IHelloWorld helloWorld=(IHelloWorld) factory.create();
//String str=helloWorld.sayHello("小魏");
Annex annex =new Annex();
annex.setDataType("不错111");
annex.setDocumentName("这是测试问333");
annex.setFileType("ces22");
annex.setLength(700);
DataSource source = new FileDataSource(new File("C:\\Users\\Administrator\\Desktop\\trafficarch.sql"));
annex.setFileData(new DataHandler(source));
String isSuccess=helloWorld.addAnnex(annex);
System.out.println(isSuccess);
/**
* 使用动态调用
*/
// JaxWsDynamicClientFactory factory =JaxWsDynamicClientFactory.newInstance();
// try {
// System.out.println(factory);
Client client =factory.createClient("http://localhost:8888/MyCxfService?wsdl");
// Client client =factory.createClient("http://localhost:8080/xzhd_oa/cxfws/hi?wsdl");
// System.out.println("client"+client);
// Object[] obj;
// obj = client.invoke("getAnnex");
// //Annex annex=(Annex) obj[0];
if(obj != null && obj.length > 0){
System.out.println(obj[0].getClass().getMethod("getDocumentName").invoke(obj[0])); <span style="font-family: Arial, Helvetica, sans-serif;"> </span><span style="font-family: Arial, Helvetica, sans-serif;">
</span> }
// //System.out.println(annex.getDocumentName());
// System.err.println("resp:"+JaxbUtils.readObjectFromXmlByContent(Annex.class,obj[0]+"").getDocumentName());
// client.destroy();
// } catch (Exception e) {
// e.printStackTrace();
// }
}
}
客户端调用