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

CXF之MTOM(传送二进制数据文件)实例 收藏
服务接口:

view plaincopy to clipboardprint?
package cxf.server; 
import javax.jws.WebService; 
@WebService 
public interface SendPicture { 
    String sendPicture(Picture picture); 

 

服务实现:

view plaincopy to clipboardprint?
package cxf.server; 
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; 
@WebService(endpointInterface = "cxf.server.SendPicture") 
public class SendPictureImpl implements SendPicture { 
    @Override 
    public String sendPicture(Picture picture) { 
         
        try {  
            DataHandler handler = picture.getImag(); 
            InputStream is = handler.getInputStream();  
            OutputStream os = new FileOutputStream(new File("D://8.gif"));  
            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();  
        }  
        System.out.println("server: " + "FHD"); 
        return "YES"; 
    } 

 

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

view plaincopy to clipboardprint?
package cxf.server; 
import javax.activation.DataHandler; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlMimeType; 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Picture { 
    @XmlMimeType("application/octet-stream") 
    private DataHandler imag; 
    public DataHandler getImag() { 
        return imag; 
    } 
    public void setImag(DataHandler imag) { 
        this.imag = imag; 
    } 

 

服务端配置:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?> 
<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="SendPicture" implementor="cxf.server.SendPictureImpl" 
        address="/SendPicture"> 
        <jaxws:properties> 
            <entry key="mtom-enabled" value="true" /> 
        </jaxws:properties> 
    </jaxws:endpoint> 
</beans> 
 

客户端配置:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?> 
<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"> 
    <jaxws:client id="client" 
    address="http://localhost:8085/java_first_spring_support1/service/SendPicture
    serviceClass="cxf.server.SendPicture"/> 
</beans> 
 

客户端调用:

view plaincopy to clipboardprint?
package cxf.client; 
import java.io.File; 
import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.FileDataSource; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import cxf.server.Picture; 
import cxf.server.SendPicture; 
 
public final class Client { 
    public static void main(String args[]) throws Exception { 
         
        ClassPathXmlApplicationContext context  = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"}); 
        SendPicture client = (SendPicture)context.getBean("client"); 
         
        Picture picture = new Picture(); 
        DataSource source = new FileDataSource(new File("F://3.gif"));  
        picture.setImag(new DataHandler(source)); 
        String str = client.sendPicture(picture); 
        System.out.println("client: " + str); 
        System.exit(0); 
    } 

 

请注意:

@XmlMimeType("application/octet-stream")

private DataHandler imag;

还有:

<jaxws:properties>

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

</jaxws:properties>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值