`
yanghui_java
  • 浏览: 5431 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

关于CXF大文件的传输问题

 
阅读更多

最近在做一个webservice的项目,本人选用的是Apache CXF来实现,因为需要再项目中进行大文件的传输问题,在网上找了很多这方面的资料,都不甚理想,说的都不够明白,有的文章讲的传输小文件还可以,在传输大文件的时候就会报内存溢出异常,这个是麻烦的事情,本人在综合了各位前辈的经验,终于实现了大文件大的传输问题,采用的是mtom的机制进行附件传输,废话少说,下面就是cxf整合spring进行大文件传输的一个例子,当然了,至于jar包就自己到官网下载了,首先开发客户端代码:

第一步:开发接口

package cn.genomics.signtureServer.cxf;

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

import cn.genomics.signtureServer.model.Resume;

@WebService
public interface IFileUpload {
	
	String upload(@WebParam(name="resume") Resume resume);
}

 @WebService和(@WebParam这两个注释是不可以少的哦。

类Resume包含传输文件的参数:文件名,文件类型,数据源(可以是Datahandler或是一个字节数组)我这用的是Datahandler,代码如下:

package cn.genomics.signtureServer.model;

import javax.activation.DataHandler;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlMimeType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="resume")
@XmlAccessorType(XmlAccessType.FIELD)
public class Resume {
	
	private String fileName;
	private String fileType;
	@XmlMimeType("application/octet-stream")
	private DataHandler fileData;
	
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileType() {
		return fileType;
	}
	public void setFileType(String fileType) {
		this.fileType = fileType;
	}
	public DataHandler getFileData() {
		return fileData;
	}
	public void setFileData(DataHandler fileData) {
		this.fileData = fileData;
	}
}

 @XmlMimeType("application/octet-stream")记住这个注释是必不可少的,说明这是一个二进制文件。

第二步:开发接口实现类:

package cn.genomics.signtureServer.cxf;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.jws.WebService;

import cn.genomics.signtureServer.model.Resume;

@WebService(endpointInterface="cn.genomics.signtureServer.cxf.IFileUpload") 
public class IFileUploadImpl implements IFileUpload {

	@Override
	public String upload(Resume resume) {
		File outFilePath = new File("F:\\上传文件");//文件上传的路径,自己想上传到哪就到哪,自己决定
		File outFile = new File(outFilePath.getAbsolutePath() + File.separator + resume.getFileName() + "."
								+ resume.getFileType());
		if(!outFilePath.exists()) {
			outFilePath.mkdir();
		}
		byte[] buf = new byte[1024 * 512];
		int read = 0;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(resume.getFileData().getInputStream());
			bos = new BufferedOutputStream(new FileOutputStream(outFile));
			while((read = bis.read(buf)) != -1) {
				bos.write(buf,0,read);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(bos != null) {
					bos.close();
				}
				if(bis != null) {
					bis.close();
				}
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
		return "文件上传成功";
	}
}

 接下来就是配置文件的配置了,首先是web.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <context-param> 
  	<param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener> 
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener> 
  
 
  <servlet>
  	<servlet-name>CXFServlet</servlet-name>
  	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 
  	<load-on-startup>1</load-on-startup>
  </servlet>
	<servlet-mapping> 
		<servlet-name>CXFServlet</servlet-name> 
		<url-pattern>/sealService/*</url-pattern> 
	</servlet-mapping> 
	
</web-app>

 下面是applicationContext.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:xsd="http://www.w3.org/2001/XMLSchema"
	   xmlns:tx="http://www.springframework.org/schema/tx" 
	   xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:http-conf="http://cxf.apache.org/transports/http/configuration" 
	   xmlns:jaxws="http://cxf.apache.org/jaxws"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
					       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
						   http://www.springframework.org/schema/aop  
           		           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
					       http://www.springframework.org/schema/context
					       http://www.springframework.org/schema/context/spring-context-3.0.xsd 
					       http://cxf.apache.org/transports/http/configuration 
					       http://cxf.apache.org/schemas/configuration/http-conf.xsd   
					       http://www.springframework.org/schema/tx   
      					   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
					       http://cxf.apache.org/jaxws
					       http://cxf.apache.org/schemas/jaxws.xsd">
	<aop:aspectj-autoproxy/>  
	<context:component-scan base-package="cn.genomics.signtureServer"/>
	<context:annotation-config/>
	  
	<import resource="classpath:META-INF/cxf/cxf.xml"/>
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
	
	
	<http-conf:destination name="*.http-destination">  
        <http-conf:server ReceiveTimeout="90000"/>  
    </http-conf:destination>  
   
	<bean id="upload" class="cn.genomics.signtureServer.cxf.IFileUploadImpl"/>
	<jaxws:endpoint id="fileUpload" implementor="#upload" address="/FileUpload" publish="true">
		<jaxws:features>
			<bean class="org.apache.cxf.feature.LoggingFeature" /> 
		</jaxws:features>  
		<jaxws:properties >
			<entry key="mtom-enabled" value="true"/>
    	</jaxws:properties>
	</jaxws:endpoint>
</beans> 

到这里服务器端就开发完毕,把项目部署到tomcat容器里,访问地址:(我的是80端口)http://localhost/testFileUpload/sealService/FileUpload?wsdl,如果能访问的到,就说明成功了。下面的任务就是要开发客户端了。

首先接口:

package cn.genomics.signtureserver.cxf;

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

@WebService(name = "IFileUpload", targetNamespace = "http://cxf.signtureServer.genomics.cn/")
@SOAPBinding(use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public interface IFileUpload {
	@WebMethod(operationName = "upload", action = "")
	@WebResult(name = "return", targetNamespace = "")
	String upload(@WebParam(name="resume")Resume resume);

}

 Resume类跟服务器端一样

接下来配置文件applicationContext_client.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:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
					http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
					http://www.springframework.org/schema/context 
					http://www.springframework.org/schema/context/spring-context-3.0.xsd 
					http://cxf.apache.org/transports/http/configuration 
					http://cxf.apache.org/schemas/configuration/http-conf.xsd    
					http://cxf.apache.org/jaxws 
					http://cxf.apache.org/schemas/jaxws.xsd">
	<import resource="classpath:META-INF/cxf/cxf.xml"/>
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
	
	<http-conf:conduit name="*.http-conduit">  
        <http-conf:client ConnectionTimeout="30000" ReceiveTimeout="60000"/>  
    </http-conf:conduit>  
	
	<bean id="client" class="cn.genomics.signtureserver.cxf.IFileUpload" factory-bean="clientFactory" factory-method="create"/>
	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass" value="cn.genomics.signtureserver.cxf.IFileUpload"/>
		<property name="address" value="http://localhost/testFileUpload/sealService/FileUpload?wsdl"/>
		<property name="properties">
			<map><entry key="mtom-enabled" value="true"></entry></map>
		</property>
	</bean>
</beans> 

 <map><entry key="mtom-enabled" value="true"></entry></map>这个绝对不能少。

现在可以测试了:

package cn.genomics.signtureserver.cxf;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	
	public static void main(String[] args) {
		ApplicationContext con = new ClassPathXmlApplicationContext("applicationContext_client.xml");
		IFileUpload iu = con.getBean("client",IFileUpload.class);
		Resume re = new Resume();
		re.setFileName("h1");
		re.setFileType("zip");
		re.setFileData(new DataHandler(new FileDataSource("D:\\华大基因\\testDemo\\t1\\h1.zip")));
		iu.upload(re);
	}

}

 

分享到:
评论

相关推荐

    基于spring+cxf实现用户文件传输的webservice

    基于spring+cxf实现用户文件传输的webservice.docx,webservice的小例子,对学习很有用,欢迎大家下载

    CXF3.0+Spring3.2 传输文件

    利用CXF3.0.2+Spring3.2.14传输文件,里面有笔者在练习过程中遇到的错误,还有源代码可以下载!

    WebService CXF --- 传输文件MTOM

    NULL 博文链接:https://exceptioneye.iteye.com/blog/1325187

    cxf客户端(对象,上传与下载文件支持断点传输)

    对象传参,上传与下载文件,支持断点传输;服务端下载在第二个资源。

    CXF服务端对象文件支持断点传送

    对象传参,上传与下载文件,支持断点传输;客户端下载在第二个资源。

    apache cxf

    什么事SOAP协议,简单来说就是两个不同项目(开发语言不同等)通过xml文件来描述要传输的东西,然后通过HTTP协议传输,接收方把收到的xml解析成需要的对象使用,返回的时候又用xml封装又通过http协议传输,如此就是...

    Webservice-CXF实用手册学习大全

    wsdl解析,soap消息格式 输入输出参数的注解,Web服务上下文 Jax—Ws异常处理,MTOM文件传输 Jax-Rs,Web服务生命周期 Cxf集成Spring 安全机制(用户命令+数字签名+混合验证) Cxf拦截器特征机制,Jax-Rs异步调用

    apache-cxf-2.3.0-src

    这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样...

    CXF3.0.2+Spring3.2.14 WebService入门实例四

    利用CXF3.0.2+Spring3.2.14传输文件,里面有笔者在练习过程中遇到的错误,还有源代码可以下载!

    springboot+webservice+cxf

    基于cxf,能够调用第三方天气接口,服务端接口可以传输普通字符串、json字符串、文件上传和下载功能,满足基本的开发需求

    CXF开发的服务端及客户端

    使用CXF+spring开发的实现文件的上传下载,可以进行视频音频的传输。可供学习使用。

    cxf:Apache CXF

    CXF包含广泛的功能集,但主要集中在以下领域: Web服务标准支持:CXF支持各种Web服务标准,包括SOAP,基本配置文件,WSDL,WS-Addressing,WS-Policy,WS-ReliableMessaging,WS-Security,WS-Securi

    ApacheCXF:Apache CXF Web服务开发代码

    的ApacheCXF Apache CXF Web服务开发 ...第4章-了解服务传输 第5章-实现高级功能 第6章-使用CXF开发RESTful服务 第7章-使用CXF部署RESTful服务 第8章-使用CXF工具 该文件夹包含文本文件,其中包含相应章节的代码。

    Dubbo简介.docx

    rmi : 采用 JDK 标准的 rmi 协议实现,传输参数和返回参数对象需要实现 Serializable 接口,使用 java 标准序列化机制,使用阻塞式短连接,传输数 据包大小混合,消费者和提供者个数差不多,可传文件,传输协议 ...

    Java思维导图xmind文件+导出图片

    CDN静态文件访问 分布式存储 分布式搜索引擎 应用发布与监控 应用容灾及机房规划 系统动态扩容 分布式架构策略-分而治之 从简到难,从网络通信探究分布式通信原理 基于消息方式的系统间通信 理解通信协议...

    camel项目实例

    Apache Camel 采用URI来描述各种组件,这样你可以很方便地与各种传输或者消息模块进行交互,其中包含的模块有 HTTP, ActiveMQ, JMS, JBI, SCA, MINA or CXF Bus API。 这些模块是采用可插拔的方式进行工作的。Apache...

    activemq的windowns编译库、centos7编译库和mac编译库(含头文件和库文件)

    ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS...支持可插拔传输协议,例如in-VM,TCP,SSL,NIO,UDP,多播,JGroups和JXTA传输 使用JDBC和高性能日志支持非常快速的持久性

    CX-测斜仪数据处理程序v8.6.1.rar

    本程序用于对测斜数据进行分析处理,形成数据报表,数据统计分析等首次运行,会在程序所在文件夹添加几个.DLL文件, 随即消失,然后程序启动,此时即可正常使用。下次在运行,就会直接启动主程序,进入数据分析。...

    ASP EXCEL导入SQL

    它们的新的项目分别是ApacheCXF和Axis2.Java语言也制定关于REST网络服务规范:JAX-RS:JavaAPIforRESTfulWebServices(JSR311)。相信还会出现更多与REST相关的激动人心的信息。  REST与AJAX技术  尽管AJAX技术的...

    ca源码java-camel:ApacheCamel是一个开源集成框架,可让您快速轻松地集成使用或生成数据的各种系统

    Camel使用URI来简化与所有传输或消息传递模型(包括HTTP,ActiveMQ,JMS,JBI,SCA,MINA或CXF)的集成,并使用可插拔数据格式选项。 Apache Camel是一个小型库,具有最小的依赖关系,可轻松嵌入任何Java应用程序中...

Global site tag (gtag.js) - Google Analytics