CXF實戰之傳輸文件(六)

CXF的文件傳輸通過MTOM實現。MTOM(SOAP Message Transmission Optimization Mechanism)SOAP消息傳輸優化機制,可以在SOAP消息中發送二進制數據。MTOM允許將消息中包含的大型數據元素外部化,並將其作爲無任何特殊編碼的二進制數據隨消息一起傳送。相對於把二進制轉爲base64進行傳輸,MTOM具有更高的傳輸效率。

文件傳輸包裝類

CXF文件傳輸DataHandler只有二進制數據,沒有文件名、文件類型和文件大小等,需要額外的傳輸參數。通常自定義文件傳輸包裝類來傳輸二進制和額外參數。

package com.rvho.cxfserver;

import javax.activation.DataHandler;
import javax.xml.bind.annotation.XmlMimeType;
import javax.xml.bind.annotation.XmlType;

/**
 * CXF上傳和下載文件對象包裝類 由於CXF的DataHandler無法獲取文件名和文件類型,需要在上傳和下載時附帶文件名
 * 
 * @author [email protected]
 */
@XmlType(name = "CxfFileWrapper")
public class CxfFileWrapper {
    //文件名
    private String fileName;
    //文件擴展名
    private String fileExtension;
    //文件二進制數據
    private DataHandler file;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileExtension() {
        return fileExtension;
    }

    public void setFileExtension(String fileExtension) {
        this.fileExtension = fileExtension;
    }

    //註解該字段爲二進制流
    @XmlMimeType("application/octet-stream")
    public DataHandler getFile() {
        return file;
    }

    public void setFile(DataHandler file) {
        this.file = file;
    }
}

服務端

文件傳輸服務接口

package com.rvho.cxfserver.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import com.rvho.cxfserver.CxfFileWrapper;


@WebService(name = "FileWS", targetNamespace = "http://www.tmp.com/services/file")
public interface FileWS {
    /**
     * 文件上傳
     * @param file 文件上傳包裝類
     * @return 上傳成功返回true,上傳失敗返回false。
     */
    @WebMethod
    boolean upload(@WebParam(name = "file") CxfFileWrapper file);

    /**
     * 文件下載
     * @return 文件
     */
    @WebMethod
    CxfFileWrapper download();
}

文件傳輸服務實現類

package com.rvho.cxfserver.ws.impl;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.jws.WebService;

import org.springframework.stereotype.Service;

import com.rvho.cxfserver.CxfFileWrapper;
import com.rvho.cxfserver.ws.FileWS;

@WebService(endpointInterface = "com.rvho.cxfserver.ws.FileWS", portName = "FileWSPort", serviceName = "FileWSService", targetNamespace = "http://www.tmp.com/services/file")
@Service("fileWS")
public class FileWSImpl implements FileWS {

    @Override
    public boolean upload(CxfFileWrapper file){
        boolean result = true;

        OutputStream os = null;
        InputStream is = null;
        BufferedOutputStream bos = null;

        try {
            is = file.getFile().getInputStream();

            // 文件在服務器上的保存位置
            File dest = new File("d:\\dev\\tmp\\upload\\" + file.getFileName());

            os = new FileOutputStream(dest);
            bos = new BufferedOutputStream(os);

            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }

            bos.flush();

        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        } finally {
            if(bos != null){
                try{
                    bos.close();
                }catch(Exception e){                    
                }
            }

            if(os != null){
                try{
                    os.close();
                }catch(Exception e){                    
                }
            }

            if(is != null){
                try{
                    is.close();
                }catch(Exception e){                    
                }
            }
        }

        return result;
    }

    @Override
    public CxfFileWrapper download() {
        //下載文件的路徑
        //String filePath = "D:\\dev\\tmp\\upload\\行政區.txt";
        String filePath = "E:\\TDDOWNLOAD\\Xme_5.0.517.exe";

        CxfFileWrapper fileWrapper = new CxfFileWrapper();
        fileWrapper.setFileName("Xme_5.0.517.exe");
        fileWrapper.setFileExtension("exe");

        DataSource source = new FileDataSource(new File(filePath));
        fileWrapper.setFile(new DataHandler(source));

        return fileWrapper;
    }

}

服務端配置文件,需要在Endpoint配置中開啓MTOM。

<jaxws:endpoint id="fileWSEndpoint" implementor="#fileWS" address="/file">
    <jaxws:inInterceptors>
        <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
    </jaxws:inInterceptors>
    <jaxws:outInterceptors>
        <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
    </jaxws:outInterceptors>
    <jaxws:properties>  
        <!-- 開啓MTOM -->
        <entry key="mtom_enabled" value="true"></entry>  
    </jaxws:properties>
</jaxws:endpoint>

客戶端

客戶端下載文件

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(FileWS.class);
factory.setAddress("http://localhost:8280/cxfserver/services/file");

FileWS fileWS = factory.create(FileWS.class);
CxfFileWrapper fileWrapper = fileWS.download();

OutputStream os = null;
InputStream is = null;
BufferedOutputStream bos = null;

try {
    is = fileWrapper.getFile().getInputStream();

    // 文件在客戶端的保存位置
    File dest = new File("d:\\dev\\tmp\\download\\" + fileWrapper.getFileName());

    os = new FileOutputStream(dest);
    bos = new BufferedOutputStream(os);

    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = is.read(buffer)) != -1) {
        bos.write(buffer, 0, len);
    }

    bos.flush();
    System.out.println("下載完成");
} catch (IOException e) {
    e.printStackTrace();
}finally{
    if(bos != null){
        try{
            bos.close();
        }catch(Exception e){
        }
    }

    if(os != null){
        try{
            os.close();
        }catch(Exception e){
        }
    }

    if(is != null){
        try{
            is.close();
        }catch(Exception e){
        }
    }
}

客戶端上傳文件

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(FileWS.class);
factory.setAddress("http://localhost:8280/cxfserver/services/file");
factory.getInInterceptors().add(new org.apache.cxf.interceptor.LoggingInInterceptor());

factory.getOutInterceptors().add(new com.rvho.cxfclient.interceptor.AuthAddInterceptor());
factory.getOutInterceptors().add(new org.apache.cxf.interceptor.LoggingOutInterceptor());

FileWS fileWS = factory.create(FileWS.class);

CxfFileWrapper fileWrapper = new CxfFileWrapper();
fileWrapper.setFileName("Xme_5.0.517.exe");
fileWrapper.setFileExtension("exe");

String filePath = "E:\\TDDOWNLOAD\\Xme_5.0.517.exe";
//String filePath = "e:\\temp\\項目產出文檔模板.zip";
DataSource source = new FileDataSource(new File(filePath));
fileWrapper.setFile(new DataHandler(source));

boolean success = fileWS.upload(fileWrapper);
System.out.println(success ? "上傳成功!" : "上傳失敗!");

注意事項

CXF不能傳輸大文件,否則會提示內存溢出。
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章