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