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 accountwcx@qq.com
*/
@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不能传输大文件,否则会提示内存溢出。