程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> CXF:通過WebService上傳文件,包括大文件的處理,cxfwebservice

CXF:通過WebService上傳文件,包括大文件的處理,cxfwebservice

編輯:JAVA綜合教程

CXF:通過WebService上傳文件,包括大文件的處理,cxfwebservice


參考網上文章,用CXF發布上傳文件接口,並上傳大文件的測試。

框架:spring3.1+cxf2.7.6

1.定義文件類實體

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

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

    public final String getFileName() {
        return fileName;
    }

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

    public final String getFileExtension() {
        return fileExtension;
    }

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

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

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

2.定義服務接口

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

import net.bwda.domain.upload.CxfFileWrapper;


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

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


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 org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import net.bwda.domain.upload.CxfFileWrapper;
import net.bwda.service.upload.FileWS;

@Service
@Component("fileWS")
public class FileWSImpl implements FileWS {

    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:\\upload\\" + file.getFileName());
            os = new FileOutputStream(dest);
            bos = new BufferedOutputStream(os);
            byte[] buffer = new byte[1024*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;
    }

    public CxfFileWrapper download() {
        //下載文件的路徑
         String filePath = "D:\\test.xlsx";
        CxfFileWrapper fileWrapper = new CxfFileWrapper();
        fileWrapper.setFileName("test.xlsx");
        fileWrapper.setFileExtension("xlsx");
        DataSource source = new FileDataSource(new File(filePath));
        fileWrapper.setFile(new DataHandler(source));
        return fileWrapper;
    }

}

3.定義CXF配置文件

<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://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
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"
    xmlns:cxf="http://cxf.apache.org/core">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <cxf:bus>
        <cxf:properties>
            <!-- 定義上傳文件的最大長度,限制為256M -->
            <entry key="org.apache.cxf.stax.maxTextLength" value="268435456" />
        </cxf:properties>
    </cxf:bus>

    <jaxws:endpoint id="fileWSEndpoint" implementor="#fileWS"
        address="/file">
        <jaxws:properties>
            <!-- 開啟MTOM -->
            <entry key="mtom_enabled" value="true"></entry>
        </jaxws:properties>
    </jaxws:endpoint>

</beans>

更多CXF配置,可參見:https://cxf.apache.org/docs/security.html。

 

4.生成客戶端代碼

d:\apache-cxf-2.7.6\bin\wsdl2java.bat -frontend jaxws21 -p com..test.webservice.file -client http://localhost:9080/filemark/webservice/file?wsdl

5.根據自動上傳的代碼上傳文件測試

/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

import java.io.File;
import java.net.URL;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;

/**
 * This class was generated by Apache CXF 2.7.6
 * 2016-11-10T18:37:44.270+08:00
 * Generated source version: 2.7.6
 * 
 */
public final class FileWS_FileWSImplPort_Client {

    private static final QName SERVICE_NAME = new QName("http://upload.serviceimpl.bwda.net/", "FileWSImplService");

    private FileWS_FileWSImplPort_Client() {
    }

    public static void main(String args[]) throws java.lang.Exception {
        URL wsdlURL = FileWSImplService.WSDL_LOCATION;
        FileWSImplService ss = new FileWSImplService(wsdlURL, SERVICE_NAME);
        FileWS port = ss.getFileWSImplPort();  
        
        {
        System.out.println("Invoking upload...");
        net.bwda.Test.webservice.CxfFileWrapper _upload_file = new CxfFileWrapper();
        _upload_file.setFileName("test.csv");
        _upload_file.setFileExtension("csv");
        DataSource source = new FileDataSource(new File("d:\\test.csv"));
        _upload_file.setFile(new DataHandler(source));
        boolean _upload__return = port.upload(_upload_file);
        System.out.println("upload.result=" + _upload__return);
        }

        System.exit(0);
    }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved