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文件了
[火星人 ] 使用CXF和MTOM上傳附件已經有784次圍觀