`
yanghui_java
  • 浏览: 5514 次
  • 性别: 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);
	}

}

 

分享到:
评论

相关推荐

    WebService CXF --- 传输文件MTOM

    总的来说,通过CXF框架实现MTOM,开发者可以提高Web服务中大文件传输的效率,降低网络负载,同时需要关注安全性、性能优化和测试环节,确保服务稳定可靠。了解并熟练掌握MTOM在CXF中的应用,对于提升Web服务的质量和...

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

    基于spring+cxf实现用户文件传输的webservice 在本文中,我们将探讨如何使用Spring+CXF实现用户文件传输的Webservice。该Webservice提供了基本的报文上传和查询功能,同时还提供了用户身份验证功能。 Spring 和 ...

    CXF3.0+Spring3.2 传输文件

    首先,让我们了解CXF3.0.2在文件传输中的作用。CXF允许开发者创建和消费各种类型的Web服务,包括SOAP和RESTful。在文件传输场景下,CXF提供了一种灵活且高效的方式,可以将文件作为服务操作的一部分进行发送和接收。...

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

    总的来说,CXF服务端对象文件支持断点传送是一项重要的特性,它提高了大文件传输的效率和可靠性,同时展示了CXF在处理复杂数据交换和文件操作方面的强大能力。开发者可以通过学习和研究这个示例,更好地理解和利用...

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

    本示例着重于CXF客户端如何处理对象参数传递以及支持文件的上传和下载,特别是实现了断点续传功能,这在大文件传输时尤为关键。 【对象传参】: CXF允许通过JAXB(Java Architecture for XML Binding)将Java对象...

    cxf webservice 文件分块上传

    总的来说,通过CXF Webservice实现文件分块上传是一个复杂但实用的系统设计,它结合了网络通信、文件操作、错误处理和安全性等多个方面的知识,对于提升大型文件传输的效率和可靠性具有重要意义。

    cxf所需JAR包和xsd文件

    在给定的压缩包文件中,"apache-cxf-3.1.4.tar.gz"和"apache-cxf-3.1.4-src.tar.gz"分别包含了CXF库的编译版和源代码。 1. **CXF的JAR包**: CXF的JAR包是运行和开发基于CXF的Web服务所必需的。这些JAR文件包含CXF...

    cxf使用中编码问题

    "cxf使用中编码问题"这个主题聚焦在如何在CXF环境中解决与GBK等特定编码相关的困扰。 首先,我们需要理解编码的本质。在计算机系统中,字符编码是用来表示文本的一种方式,如ASCII、GBK、UTF-8等。GBK是中国大陆...

    java cxf规范rest

    总的来说,"java cxf规范rest"是一个关于使用CXF框架构建RESTful服务的议题,涵盖了从服务接口定义、服务实现、HTTP传输配置到遵循编码规范等一系列步骤。开发者需要熟悉JAX-WS和JAX-RS,理解RESTful设计原则,并能...

    CXF利用Json格式传输对象

    本篇文章将深入探讨如何利用CXF框架在Java环境中以JSON格式传输对象。 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,因其简洁性和易读性而受到开发者喜爱。在CXF中,我们可以方便地将Java对象...

    cxf所需jar包

    "2积分.txt"可能是一个文本文件,可能包含了关于这些JAR包的额外信息,如积分系统或者获取这些资源的说明。然而,由于这是一个非标准的文件名,它不太可能是CXF框架的一部分。在实际项目中,我们更期望看到的是诸如...

    Soap with attachment的cxf实现方法java

    MTOM是一种优化XML附件内容传输的方式,它允许二进制大对象以二进制的形式在SOAP消息中传输,而不是将它们编码为XML,这样可以显著减小SOAP消息的大小。 通过添加自定义拦截器,我们可以在SOAP消息发送之前处理权限...

    cxf传输json

    标题 "cxf传输json" 涉及到的是Apache CXF框架在处理JSON数据传输方面的应用。Apache CXF是一个开源的Java框架,主要用于构建和开发服务导向架构(SOA)和Web服务。在这个场景中,CXF被用来作为客户端和服务端之间...

    CXF应用整理(包括大文件上传MTOM、安全机制)

    综上所述,CXF 提供了一套完整的工具和组件,可帮助开发者快速构建和部署高质量的 Web 服务,支持从简单的字符串传递到复杂的对象交换,以及安全性和大文件传输等高级特性。通过使用 CXF,开发人员可以更加专注于...

    apache-cxf-2.4.6.zip

    6. **强大的调试工具**:CXF提供了客户端和服务器端的调试器,帮助开发者追踪和解决Web服务问题。 7. **集成其他Apache项目**:CXF与Apache Axis2、Tomcat、Jetty等项目有良好的集成,可以方便地嵌入到现有的应用...

    cxf 3.1.1 jar包

    在压缩包“cxf3.1.1”中,你可能会找到以下关键文件或目录: 1. **cxf-api.jar**: 包含CXF的主要API接口和抽象类,这些接口定义了如何与CXF框架交互。 2. **cxf-rt-frontend-jaxws.jar**: 提供了JAX-WS(Java API ...

    cxf的jar包

    7. **国际化支持**:CXF支持多语言环境,能够处理不同地区的字符编码问题。 8. **插件体系**:CXF拥有丰富的插件体系,开发者可以扩展其功能来满足特定需求。 在“lib”目录下的文件,很可能是CXF框架的各种依赖库...

    Apache CXF文件目录结构及需要jar包

    在了解Apache CXF的文件目录结构以及所需JAR包之前,我们首先需要理解CXF的基本工作原理。 Apache CXF主要由以下几个组件构成: 1. **Bus**:这是CXF的核心,负责管理服务、消息处理器和其他组件。 2. **Endpoint**...

    CXF生成的WSDL详解

    ### CXF生成的WSDL详解 #### 一、概述 WSDL(Web Services Description Language)是一种用于描述Web服务的标准XML格式。...此外,熟悉WSDL文件的结构也有助于在遇到问题时能够快速定位问题所在,从而提高开发效率。

    cxf基本jar包

    以上就是关于CXF基本jar包的详细说明。在实际开发中,根据项目的具体需求,可能还需要其他依赖库,如Spring框架的jar包,以便与CXF集成。在使用CXF时,确保正确配置和管理这些jar文件,将有助于构建稳定、高效的Web...

Global site tag (gtag.js) - Google Analytics