Fileentity.java
package com.yunix.service.entity; import javax.activation.DataHandler; /** * 文件实体类 * @author yunix * 2013-3-2 上午11:19:22 */ public class FileEntity { private String fileName; private String fileType; private DataHandler file; 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 getFile() { return file; } public void setFile(DataHandler file) { this.file = file; } }
IFileService.java
package com.yunix.service.webservice; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.xml.ws.soap.MTOM; import com.yunix.service.entity.FileEntity; /** * 文件service接口 * @author yunix * 2013-3-2 上午11:22:22 */ @WebService @MTOM public interface IFileService { @WebMethod public void uploadFile(@WebParam(name="fileEntity")FileEntity fileEntity); }
FileService.java
package com.yunix.service.webservice; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.activation.DataHandler; import com.yunix.service.entity.FileEntity; /** * 文件Service实现 * @author yunix * 2013-3-2 上午11:24:57 */ public class FileService implements IFileService { @Override public void uploadFile(FileEntity fileEntity) { System.out.println("1"); DataHandler handler = fileEntity.getFile(); System.out.println("2"); try { InputStream is = handler.getInputStream(); OutputStream os = new FileOutputStream(new File("F:/"+fileEntity.getFileName()+"."+fileEntity.getFileType())); byte[] b = new byte[100000]; int bytesRead = 0; while((bytesRead = is.read(b)) != -1){ os.write(b, 0,bytesRead); } System.out.println("3"); os.flush(); os.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } }
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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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-servlet.xml" /> <jaxws:endpoint id="file" implementor="com.yunix.service.webservice.FileService" address="/fileService"> </jaxws:endpoint> </beans>
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"> <display-name>cxf</display-name> <description>Apache CXF Endpoint</description> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:cxf-servlet.xml</param-value> </context-param> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
启动并运行项目
http://localhost:8080/service-file/fileService?wsdl
可查看wsdl
测试代码:
package com.test; import java.io.File; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.yunix.service.entity.FileEntity; import com.yunix.service.webservice.IFileService; public class Test { public static void main(String[] args) throws Exception { String url = "http://localhost:8080/service-file/fileService?wsdl"; FileEntity file = new FileEntity(); file.setFileName("2012-03-04TestFile"); file.setFileType("jpg"); DataSource source = new FileDataSource(new File("d:\\123456.jpg")); file.setFile(new DataHandler(source)); JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(IFileService.class); factory.setAddress(url); IFileService client = (IFileService) factory.create(); try { client.uploadFile(file); } catch (Exception e) { System.out.println(e); } System.out.println("success"); } }
相关推荐
cxf rest最简实例可直接运行,先运行服务,在浏览器中输入项目路径http://localhost:8080/webrest/api/str/abc进行测试
cxf spring maven 实例 webservice
CXF实现WebService详解实例
这是一个cxf安全验证的完整实例,直接可以部署到应用服务器中运行。
springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传springboot cxf下载上传...
cxf+sprinp webservice实现带测试程序,以验证
使用CXF开发简单HelloWorld实例
CXF结合spring项目实例,com.email.ws.impl包下为eclipse工具生成的客户端,text包用来测试
Spring + cxf = webservice 完整实例源码免费下载 完全免费。此资源仅为文档提供。 版权为百度文档 "Spring + cxf = webservice 完整实例源码免费下载" 所有。
cxf+spring webService实例,用到的包自己导入吧!
ssh+cxf整合发布webservice实例,详细解释配置过程,下载解压即可使用。
CXF开发实例.pdf
Spring CXF Restful 实例
cxf开发webservice简单实例:包括服务端与客户端并所有需要的jar,不要分啦。
CXF SOAP 应用实例
Web Service学习-CXF开发Web Service实例demo(一)
Apache CXF部署WebServices实例代码
1):含有spring-3.2.8.RELEASE.jar、velocity-tools-2.0.jar、velocity-1.7.jar、mybatis-spring-1.0.2.jar、mybatis-3.0.6.jar、cxf-api-2.2.9.jar等丰富的jar包。 2):采用spring3.2作为bean容器管理,...
CXF开发实例.CXF开发实例.CXF开发实例.CXF开发实例.CXF开发实例.CXF开发实例.
springmvc+webservice(cxf)+maven 完整实例,下载直接可以运行,非常实用