CXF之MTOM(傳送位元據檔案)執行個體

來源:互聯網
上載者:User

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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.