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.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;
}
}
参考:
本文介绍如何利用CXF结合MTOM(SOAP消息传输优化机制)进行文件的上传和下载。CXF的DataHandler类在传输文件时缺乏文件名和类型等信息,因此需要自定义CxfFileWrapper类来封装文件名、扩展名和二进制数据。在服务端,通过DataHandler获取文件输入流并保存到服务器,而在客户端则提供文件下载服务。
405

被折叠的 条评论
为什么被折叠?



