使用CXF和MTOM上传附件
来源:互联网 发布:茂名软件开发公司 编辑:程序博客网 时间:2024/05/01 19:36
实现上传一个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文件了
- 使用CXF和MTOM上传附件
- 使用Axis2传输附件(AXIS2 MTOM)
- 使用Axis2传输附件(AXIS2 MTOM)
- 使用MTOM传输附件+WS-Security
- spring-ws 使用mtom传输附件
- webservices使用MTOM附件抛出的内存溢出
- cxf 中的 MTOM
- 使用CXF附件的注意事项
- WebService CXF --- 传输文件MTOM
- 使用smartUpload上传附件
- 使用smartUpload上传附件
- 使用Part上传附件
- CXF之MTOM(传送二进制数据文件)
- CXF之MTOM(传送二进制数据文件)实例
- CXF之MTOM(传送二进制数据文件)实例
- Apache CXF Spring SOAP MTOM example
- nodejs使用multer上传附件
- 【Js--代码模块】多附件上传和修改附件
- struts2中处理自定义异常的方法小结
- oracle小结1
- FLEX 小结2
- apache common email组件小结
- flex 小结1
- 使用CXF和MTOM上传附件
- 20个超级优秀的AJAX效果
- 一大学女孩网上开店创业两年成富姐
- sql code zz...
- 查找linux系统中大文件的方法
- 业务员,你跟随的老板是精明的还是傻子?
- 我(webabcd)的文章索引
- ESBasic 可复用的.NET类库(11) -- 双向映射 IBidirectionalMapping
- ESBasic 可复用的.NET类库(10) -- 简易的读写锁 SmartRWLocker