CXF之MTOM(传送二进制数据文件)实例

http://blog.csdn.net/fhd001/article/details/5789406


服务接口:


[java] view plain copy
  1. package cxf.server;  

  2. import javax.jws.WebService;  

  3. @WebService

  4. publicinterface SendPicture {  

  5.    String sendPicture(Picture picture);  

  6. }  



服务实现:


[java] view plain copy
  1. package cxf.server;  

  2. import java.io.File;  

  3. import java.io.FileOutputStream;  

  4. import java.io.IOException;  

  5. import java.io.InputStream;  

  6. import java.io.OutputStream;  

  7. import javax.activation.DataHandler;  

  8. import javax.jws.WebService;  

  9. @WebService(endpointInterface = "cxf.server.SendPicture")  

  10. publicclass SendPictureImpl implements SendPicture {  

  11. @Override

  12. public String sendPicture(Picture picture) {  

  13. try {  

  14.            DataHandler handler = picture.getImag();  

  15.            InputStream is = handler.getInputStream();  

  16.            OutputStream os = new FileOutputStream(new File("D://8.gif"));  

  17. byte[] b = newbyte[100000];  

  18. int bytesRead = 0;  

  19. while ((bytesRead = is.read(b)) != -1) {  

  20.                os.write(b, 0, bytesRead);  

  21.            }  

  22.            os.flush();  

  23.            os.close();  

  24.            is.close();  

  25.        } catch (IOException e) {  

  26.            e.printStackTrace();  

  27.        }  

  28.        System.out.println("server: " + "FHD");  

  29. return"YES";  

  30.    }  

  31. }  



传输的数据对象,注意DataHandler这个属性:


[java] view plain copy
  1. package cxf.server;  

  2. import javax.activation.DataHandler;  

  3. import javax.xml.bind.annotation.XmlAccessType;  

  4. import javax.xml.bind.annotation.XmlAccessorType;  

  5. import javax.xml.bind.annotation.XmlMimeType;  

  6. @XmlAccessorType(XmlAccessType.FIELD)  

  7. publicclass Picture {  

  8. @XmlMimeType("application/octet-stream")  

  9. private DataHandler imag;  

  10. public DataHandler getImag() {  

  11. return imag;  

  12.    }  

  13. publicvoid setImag(DataHandler imag) {  

  14. this.imag = imag;  

  15.    }  

  16. }  



服务端配置:


[xhtml] view plain copy
  1. <?xmlversion="1.0"encoding="UTF-8"?>

  2. <beansxmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jaxws="http://cxf.apache.org/jaxws"

  4. xsi:schemaLocation="  

  5.    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  

  6.    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

  7. <importresource="classpath:META-INF/cxf/cxf.xml"/>

  8. <importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>

  9. <importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>

  10. <jaxws:endpointid="SendPicture"implementor="cxf.server.SendPictureImpl"

  11. address="/SendPicture">

  12. <jaxws:properties>

  13. <entrykey="mtom-enabled"value="true"/>

  14. </jaxws:properties>

  15. </jaxws:endpoint>

  16. </beans>



客户端配置:


[xhtml] view plain copy
  1. <?xmlversion="1.0"encoding="UTF-8"?>

  2. <beansxmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jaxws="http://cxf.apache.org/jaxws"

  4. xsi:schemaLocation="  

  5.    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  

  6.    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

  7. <jaxws:clientid="client"

  8. address="http://localhost:8085/java_first_spring_support1/service/SendPicture"

  9. serviceClass="cxf.server.SendPicture"/>

  10. </beans>



客户端调用:


[java] view plain copy
  1. package cxf.client;  

  2. import java.io.File;  

  3. import javax.activation.DataHandler;  

  4. import javax.activation.DataSource;  

  5. import javax.activation.FileDataSource;  

  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  

  7. import cxf.server.Picture;  

  8. import cxf.server.SendPicture;  

  9. publicfinalclass Client {  

  10. publicstaticvoid main(String args[]) throws Exception {  

  11.        ClassPathXmlApplicationContext context  = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});  

  12.        SendPicture client = (SendPicture)context.getBean("client");  

  13.        Picture picture = new Picture();  

  14.        DataSource source = new FileDataSource(new File("F://3.gif"));  

  15.        picture.setImag(new DataHandler(source));  

  16.        String str = client.sendPicture(picture);  

  17.        System.out.println("client: " + str);  

  18.        System.exit(0);  

  19.    }  

  20. }  



请注意:


@XmlMimeType("application/octet-stream")

private DataHandler imag;



还有:


<jaxws:properties>

<entry key="mtom-enabled" value="true" />

</jaxws:properties>


你可能感兴趣的:(接口,二进制,import)