CXF的文件传输通过MTOM实现。MTOM(SOAP Message Transmission Optimization Mechanism)SOAP消息传输优化机制,可以在SOAP消息中发送二进制数据。MTOM允许将消息中包含的大型数据元素外部化,并将其作为无任何特殊编码的二进制数据随消息一起传送。相对于把二进制转为base64进行传输,MTOM具有更高的传输效率。
文件传输包装类
CXF文件传输DataHandler只有二进制数据,没有文件名、文件类型和文件大小等,需要额外的传输参数。通常自定义文件传输包装类来传输二进制和额外参数。
package com.study.cxffile; import javax.activation.DataHandler; import javax.xml.bind.annotation.XmlMimeType; import javax.xml.bind.annotation.XmlType; /** * CXF上传和下载文件对象包装类 由于CXF的DataHandler无法获取文件名和文件类型,需要在上传和下载时附带文件名 */ @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.study.cxffile.ws; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import com.study.cxffile.CxfFileWrapper; @WebService(name = "FileWS", targetNamespace = "http://www.hellocxffile.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.study.cxffile.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.study.cxffile.CxfFileWrapper; import com.study.cxffile.ws.FileWS; @WebService(endpointInterface = "com.study.cxffile.ws.FileWS", portName = "FileWSPort", serviceName = "FileWSService", targetNamespace = "http://www.hellocxffile.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("e:\\testTemp\\" + 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 = "E:\\testTemp\\cxfspringdemo1-0.0.1-SNAPSHOT.war"; CxfFileWrapper fileWrapper = new CxfFileWrapper(); fileWrapper.setFileName("cxfspringdemo1-0.0.1-SNAPSHOT.war"); fileWrapper.setFileExtension("war"); DataSource source = new FileDataSource(new File(filePath)); fileWrapper.setFile(new DataHandler(source)); return fileWrapper; } }
spring配置beans.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- <context:annotation-config /> --> <!-- 使Spring支持自动检测组件,如注解的Repository、Service、Controller --> <context:component-scan base-package="com.study" /> </beans>
服务端配置文件,需要在Endpoint配置中开启MTOM,cxf-servlet.xml
<?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" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- fileWS通过注解获取 --> <jaxws:endpoint id="fileWSEndpoint" implementor="#fileWS" address="/file"&g