使用CXF和MTOM上傳附件

來源:互聯網
上載者:User
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檔案了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.