webService项目
FileEntity.java
package com.yunix.service.entity; import javax.activation.DataHandler; /** * 文件实体类 * @author yunix * 2013-3-2 上午11:19:22 */ public class FileEntity { private String fileName; private String fileType; private DataHandler file; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFileType() { return fileType; } public void setFileType(String fileType) { this.fileType = fileType; } public DataHandler getFile() { return file; } public void setFile(DataHandler file) { this.file = file; } }
IFileService.java
package com.yunix.service.webservice; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.xml.ws.soap.MTOM; import com.yunix.service.entity.FileEntity; /** * 文件service接口 * @author yunix * 2013-3-2 上午11:22:22 */ @WebService @MTOM public interface IFileService { @WebMethod public void uploadFile(@WebParam(name="fileEntity")FileEntity fileEntity); }
FileService
package com.yunix.service.webservice; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.activation.DataHandler; import com.yunix.service.entity.FileEntity; /** * 文件Service实现 * @author yunix * 2013-3-2 上午11:24:57 */ public class FileService implements IFileService { @Override public void uploadFile(FileEntity fileEntity) { System.out.println("1"); DataHandler handler = fileEntity.getFile(); System.out.println("2"); try { InputStream is = handler.getInputStream(); OutputStream os = new FileOutputStream(new File("F:/"+fileEntity.getFileName()+"."+fileEntity.getFileType())); byte[] b = new byte[100000]; int bytesRead = 0; while((bytesRead = is.read(b)) != -1){ os.write(b, 0,bytesRead); } System.out.println("3"); os.flush(); os.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } }
WsAuthHandler.java
package com.yunix.service.auth; import java.io.IOException; import javax.security.auth.callback.Callback; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.callback.UnsupportedCallbackException; import javax.xml.soap.SOAPException; import org.apache.cxf.interceptor.Fault; import org.apache.ws.security.WSPasswordCallback; /** * 密码回调处理类 * @author yunix * 2013-3-4 上午10:59:30 */ public class WsAuthHandler implements CallbackHandler { @Override public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { WSPasswordCallback pc = (WSPasswordCallback)callbacks[0]; if (!"file_client".equals(pc.getIdentifier())) { SOAPException soapExc = new SOAPException(""); throw new Fault(soapExc); } int usage = pc.getUsage(); if (usage == WSPasswordCallback.USERNAME_TOKEN) { pc.setPassword("123456"); } } }
cxf-servlet.xml
<?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:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <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" /> <bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" /> <bean id="wss4jInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor"> <constructor-arg> <map> <entry key="action" value="UsernameToken" /> <entry key="passwordType" value="PasswordText" /> <entry key="user" value="admin"/> <entry key="passwordCallbackClass" value="com.yunix.service.auth.WsAuthHandler" /> </map> </constructor-arg> </bean> <jaxws:endpoint id="file" implementor="com.yunix.service.webservice.FileService" address="/fileService"> <jaxws:inInterceptors> <ref bean="loggingInInterceptor"/> <ref bean="wss4jInInterceptor"/> </jaxws:inInterceptors> </jaxws:endpoint> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>cxf</display-name> <description>Apache CXF Endpoint</description> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:cxf-servlet.xml</param-value> </context-param> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
=================================
webservice完成
下面是测试项目
package com.yunix.service.auth; import java.io.IOException; import javax.security.auth.callback.Callback; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.callback.UnsupportedCallbackException; import org.apache.ws.security.WSPasswordCallback; /** * 密码回调处理类 * @author yunix * 2013-3-4 上午10:59:30 */ public class WsClientAuthHandler implements CallbackHandler { @Override public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { WSPasswordCallback pc = (WSPasswordCallback)callbacks[0]; pc.setIdentifier("file_client"); pc.setPassword("123456"); } }
拷贝FileEntity.java、IFileService.java到客户端项目
xml配置文件如下:
<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" />
<bean id="wss4JOutInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor"> <constructor-arg> <map> <entry key="action" value="UsernameToken" /> <entry key="passwordType" value="PasswordText" /> <entry key="user" value="admin" /> <entry key="passwordCallbackClass" value="com.yunix.service.auth.WsClientAuthHandler" /> </map> </constructor-arg> </bean> <jaxws:client id="ws-file" address="http://localhost:8080/service-file/fileService" serviceClass="com.yunix.service.webservice.IFileService"> <jaxws:outInterceptors> <ref bean="wss4JOutInterceptor" /> </jaxws:outInterceptors> </jaxws:client>
测试代码
package com; import java.io.File; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import org.junit.Test; import org.unitils.UnitilsJUnit4; import org.unitils.spring.annotation.SpringApplicationContext; import org.unitils.spring.annotation.SpringBeanByType; import com.yunix.service.entity.FileEntity; import com.yunix.service.webservice.IFileService; /** * 类说明 * @author yunix * 2013-3-4 下午01:13:49 */ @SpringApplicationContext("spring.xml") public class FileServiceTest extends UnitilsJUnit4 { @SpringBeanByType private IFileService fileService; @Test public void testFile(){ FileEntity file = new FileEntity(); file.setFileName("2012-03-04TestFile"); file.setFileType("jpg"); DataSource source = new FileDataSource(new File("d:\\123456.jpg")); file.setFile(new DataHandler(source)); fileService.uploadFile(file); System.out.println("success"); } }
相关推荐
springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传...
CXF实现SSL安全验证,实现https的WebService
CXF 文件记录报文日志,非控制台打印。附件带工程源代码。
CXF框架约束文件,用于导入catelog,CXF是apache旗下的开源框架,由Celtix + XFire这两门经典的框架合成,是一套非常流行的web service框架。
java CXF客户端请求ERP SAP webservice通过用户名和密码验证功能!
Web Service Cxf转换器的示例,包含源码和jar包。
验证客户端源码cxf,已经验证是可以用的,
最完整的的cxf与spring整合需要的jar文件
这是一个cxf安全验证的完整实例,直接可以部署到应用服务器中运行。
wsdl2java使用拉取cxf文件资源
利用CXF3.0.2+Spring3.2.14传输文件,里面有笔者在练习过程中遇到的错误,还有源代码可以下载!
Springboot整合CXF发布Web service和客户端调用(用户和密码验证)打开及用
WebService CXF 详细教程 加上自带CXF文件
因为前一个资源,忘记删除个人的资料了,现在重新传一个,供大家学习使用,互相交流,资源内容 为rar格式的 项目,cxf 为服务器端,cxfTest 为客户端,里边有很多调用方式,还有身份验证,使用时,搭配好 客户端和...
使用jdk1.6、cxf2.3和tomcat开发的一个关于验证码的webservice接口,主要实现对手机验证码的验证。
NULL 博文链接:https://yufenfei.iteye.com/blog/1685910
cxf 实现小例子,客户端和服务端都已经做好了,下载下来即可使用,还包含cxf所有的jar
基于SSL验证的Apache CXF客户端设计 1,服务器端Tomcat配置SSL支持 2,服务器端Web Service接口设计 3,客户端访问设计,包括代码和配置文件 详情请看博客:...
cxf rest webervice 实现文件下载
NULL 博文链接:https://openstudy.iteye.com/blog/1048441