首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

使用CXF和MTOM下传附件

2012-11-10 
使用CXF和MTOM上传附件CXF是一个不错的开源的WS框架,支持多种WS协议,其中包括对附件上传的协议MTOM,下文以

使用CXF和MTOM上传附件

CXF是一个不错的开源的WS框架,支持多种WS协议,其中包括对附件上传的协议MTOM,下文以一个例子来说明,如何用CXF和MTOM来
实现上传一个WORD的文件到服务端。
首先是服务端WS的实现。我们编写一个POJO,来处理一个待上传的简历:
Resume.java

import javax.activation.DataHandler;?

public class Resume
{
? private String candidateName;
? private String resumeFileType;
? private DataHandler resume;
。。。。。。
这里注意使用DataHandler来处理待上传的简历WORD文件
接口:ResumeUploadService.java

import javax.jws.WebParam;
import javax.jws.WebService;

import com.thea.dto.Resume;

@WebService
public interface ResumeUploadService {
???
??? void uploadResume(@WebParam(name="resume") Resume resume);
}

? 这里使用了jax-ws规范的注释去实现
实现类:
?

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 javax.jws.WebService;

import com.thea.dto.Resume;

@WebService(endpointInterface = "com.thea.service.ResumeUploadService",
?serviceName = "ResumeService")
public class ResumeUploadServiceImpl implements ResumeUploadService {

??? public void uploadResume(Resume resume) {

?DataHandler handler = resume.getResume();
?try {
???? InputStream is = handler.getInputStream();

???? OutputStream os = new FileOutputStream(new File("c:\"
????? + resume.getCandidateName() +"."+
????? resume.getResumeFileType()));
???? byte[] b = new byte[100000];
???? int bytesRead = 0;
???? while ((bytesRead = is.read(b)) != -1) {
??os.write(b, 0, bytesRead);
???? }
???? os.flush();
???? os.close();
???? is.close();

?} catch (IOException e) {
???? e.printStackTrace();
?}

??? }
}

然后在src目录下建立cxf.xml,做为服务端的配置文件

<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" />
? <jaxws:endpoint id="uploadresume"
????????????????? implementor="com.thea.service.ResumeUploadServiceImpl"
????????????????? address="/UploadResumeWS">
????????????????? <jaxws:properties>
????? <entry key="mtom-enabled" value="true"/>
??? </jaxws:properties>?
??? </jaxws:endpoint>
</beans>

这里注意使用了?? <entry key="mtom-enabled" value="true"/>设置使用MTOM附件

接下来设计客户端:
作为客户端,必须首先有Resume的POJO类,以及还有服务端的接口ResumeUploadService,设计的Client如下:

? public static void main(String args[]) throws Exception {

??? ?JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

??? ?factory.getInInterceptors().add(new LoggingInInterceptor());
??? ?factory.getOutInterceptors().add(new LoggingOutInterceptor());
??? ?factory.setServiceClass(ResumeUploadService.class);
??? ?factory.setAddress
??? ?("http://localhost:8085/CxfUploadService/services/UploadResumeWS");
??? ?ResumeUploadService client = (ResumeUploadService) factory.create();

??? ?Resume resume=new Resume();
??? ?resume.setCandidateName("KarthikeyanC");
??? ?resume.setResumeFileType("doc");
??? ?
??? ?DataSource source = new FileDataSource(new File("d:\\upload.doc"));
??? ?resume.setResume(new DataHandler(source));
??? ?client.uploadResume(resume);
??? ?System.exit(0);

??? }

注意这里由于使用了JaxWsProxyFactoryBean,并在程序中设定了对WS的各类指定,所以不用再写客户端的WS文件了

热点排行