熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

使用CXF和MTOM上傳附件

2013-11-23 19:32:23  來源: Java核心技術 

  CXF是一個不錯的開源的WS框架支持多種WS協議其中包括對附件上傳的協議MTOM下文以一個例子來說明如何用CXF和MTOM來

  實現上傳一個WORD的文件到服務端

  首先是服務端WS的實現我們編寫一個POJO來處理一個待上傳的簡歷

  Resumejava

  import javaxactivationDataHandler;

  public class Resume

  {

  private String candidateName;

  private String resumeFileType;

  private DataHandler resume;

  

  這裡注意使用DataHandler來處理待上傳的簡歷WORD文件

  接口ResumeUploadServicejava

  import javaxjwsWebParam;

  import javaxjwsWebService;

  import comtheadtoResume;

  @WebService

  public interface ResumeUploadService {

  void uploadResume(@WebParam(name=resume) Resume resume);

  }

  這裡使用了jaxws規范的注釋去實現

  實現類

  import javaioFile;

  import javaioFileOutputStream;

  import javaioIOException;

  import javaioInputStream;

  import javaioOutputStream;

  import javaxactivationDataHandler;

  import javaxjwsWebService;

  import comtheadtoResume;

  @WebService(endpointInterface = comtheaserviceResumeUploadService

  serviceName = ResumeService)

  public class ResumeUploadServiceImpl implements ResumeUploadService {

  public void uploadResume(Resume resume) {

  DataHandler handler = resumegetResume();

  try {

  InputStream is = handlergetInputStream();

  OutputStream os = new FileOutputStream(new File(c:\\

  + resumegetCandidateName() ++

  resumegetResumeFileType()));

  byte[] b = new byte[];

  int bytesRead = ;

  while ((bytesRead = isread(b)) != ) {

  oswrite(b bytesRead);

  }

  osflush();

  osclose();

  isclose();

  } catch (IOException e) {

  eprintStackTrace();

  }

  }

  }

  然後在src目錄下建立cxfxml做為服務端的配置文件

  <beans xmlns=

  xmlns:xsi=instance

  xmlns:jaxws=

  xsi:schemaLocation=

  beansxsd

  

  >

  <import resource=classpath:METAINF/cxf/cxfxml />

  <import resource=classpath:METAINF/cxf/cxfextensionsoapxml/>

  <import resource=classpath:METAINF/cxf/cxfservletxml />

  <jaxws:endpoint id=uploadresume

  implementor=comtheaserviceResumeUploadServiceImpl

  address=/UploadResumeWS>

  <jaxws:properties>

  <entry key=mtomenabled value=true/>

  </jaxws:properties>

  </jaxws:endpoint>

  </beans>

  這裡注意使用了   <entry key=mtomenabled value=true/>設置使用MTOM附件

  接下來設計客戶端

  作為客戶端必須首先有Resume的POJO類以及還有服務端的接口ResumeUploadService設計的Client如下

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

  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

  factorygetInInterceptors()add(new LoggingInInterceptor());

  factorygetOutInterceptors()add(new LoggingOutInterceptor());

  factorysetServiceClass(ResumeUploadServiceclass);

  factorysetAddress

  (//localhost:/CxfUploadService/services/UploadResumeWS);

  ResumeUploadService client = (ResumeUploadService) factorycreate();

  Resume resume=new Resume();

  resumesetCandidateName(KarthikeyanC);

  resumesetResumeFileType(doc);

  DataSource source = new FileDataSource(new File(d:\\uploaddoc));

  resumesetResume(new DataHandler(source));

  clientuploadResume(resume);

  Systemexit();

  }

  注意這裡由於使用了JaxWsProxyFactoryBean並在程序中設定了對WS的各類指定所以不用再寫客戶端的WS文件了


From:http://tw.wingwit.com/Article/program/Java/hx/201311/27050.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.