本文直接采用CXF+spring完成的一个支持上传文件的demo,包括视频、音频等,示例简单仅供参考。下面直接上代码:
一、首先进行服务端搭建(CXF+Spring):
1、新建web工程(示例为CXFServer)
2、导入所需的jar包,示例工程采用cxf3.0 可到官网上下载或者到 http://download.csdn.net/detail/erris/9370346 下载,本人较懒,就将lib下jar包全部导入,省事操作。
3工程结构
4、web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>CXFServer</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:cxf-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <init-param> <param-name>config-location</param-name> <param-value>classpath:cxf-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/cxf/*</url-pattern> </servlet-mapping> </web-app>
5、Iservice接口。
@WebService @BindingType(value=SOAPBinding.SOAP12HTTP_BINDING) public interface IService { @WebMethod(operationName = "download") public FileSources download(@WebParam(name="fileName")String fileName); @WebMethod(operationName = "upload") public String upload(@WebParam(name="file")FileSources file);
@WebService(endpointInterface = "com.git.service.IService") public class ServiceImpl implements IService{ @Override public String upload(FileSources file) { DataHandler handler = file.getHandler(); InputStream is = null; OutputStream os = null; try { System.out.println("文件开始上传。。。。。。"); is = handler.getInputStream(); os = new FileOutputStream("F:/" + file.getFileName() + "." + file.getContentType()); int n = 0; byte[] b = new byte[1024]; while ((n = is.read(b)) != -1) { os.write(b, 0, n); } os.flush(); System.out.println("文件上传成功~"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } } return "上传成功!"; } @Override public FileSources download(String fileName) { FileSources file =new FileSources(); String url ="f:/"+fileName+".jpg"; file.setFileName(fileName); file.setContentType("jpg"); try { DataSource ds = new FileDataSource(new File(url)); file.setHandler(new DataHandler(ds)); return file; } catch (Exception e) { return null; } }
@XmlRootElement(name="FileSources") @XmlAccessorType(XmlAccessType.FIELD) @XmlSeeAlso({FileSources.class}) public class FileSources implements Serializable{ private static final long serialVersionUID = -5236565563763834569L; private int id; private String fileName;// 文件名 private String contentType;// 文件类型 private DataHandler handler;//大型文件上传所需
<?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" /> <bean id="hello" class="com.git.service.impl.ServiceImpl" /> <!-- 加载提供的服务 --> <jaxws:endpoint id="service" address="/service" implementor="#hello"> <!-- 配置拦截器 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" /> </jaxws:inInterceptors> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" /> </jaxws:outInterceptors> </jaxws:endpoint> </beans>到此服务端代码都完成了,将工程部署到tomcat中,在浏览器中输入http://localhost:8080/CXFServer/cxf/service?wsdl
二、客户端(CXFClient)
客户端采用web工程,结构为:
2、IServer和JavaBean 都和服务端一样。
3、Spring配置文件配置
<?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:p="http://www.springframework.org/schema/p" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"> <bean id="client" class="com.git.service.IService" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.git.service.IService" /> <property name="address" value="http://localhost:8080/CXFServer/cxf/service" /> </bean> </beans>4、测试类
ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml"); // --------------------------[上传]---------------------------- FileSources fs = new FileSources(); fs.setFileName("3"); fs.setContentType("avi"); //加载文件 DataSource source = new FileDataSource(new File("d:\\3.avi")); fs.setHandler(new DataHandler(source)); IService client = (IService) ctx.getBean("client"); try { String result = client.upload(fs); // 此处不能传一个空对象,否则会出错 System.out.println(result); System.out.println("结果:" + result); } catch (Exception e) { System.out.println("请选择文件!\n"); System.out.println("抛出异常为" + e.getMessage()); } // --------------------------【END】------------------------到此
到此客户端编写结束,将服务端和客户端部署到tomcat,启动,执行客户端测试类,可实现上传。路径什么根据自己情况改一下。
这只是一个学习型的demo,代码什么都比较简单,仅供参考~谢谢
源代码下载地址:客户端+服务端(不含jar包):http://download.csdn.net/detail/erris/9370527