- 浏览: 1089835 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
skyesx:
这是2PC实现,更常用的是一个柔性事务的实现,可以参考http ...
Spring分布式事务实现 -
ddbird:
这第一句就不严谨“分布式事务是指操作多个数据库之间的事务”,显 ...
Spring分布式事务实现 -
呵呵6666:
基于互联网支付系统的微服务架构分布式事务解决方案http:// ...
Spring分布式事务实现 -
小黄牛:
写得不错,交流群:472213887
Spring分布式事务实现 -
jiaoqf321456:
这明明是用的apache的压缩,给ant.jar有半毛钱关系吗 ...
使用ant.jar进行文件zip压缩
一、接口与实现
二、相关POJO
三、发布WebService服务
四、客户端spring配置
五、测试代码
@WebService public interface IUserLogin { public boolean login(String userId, String password); public boolean logout(String userId); public boolean checkLoginState(String userId); }
@Repository @WebService(endpointInterface = "net.log_cd.ws.IUserLogin") public class UserLoginImpl implements IUserLogin{ @Resource private WebServiceContext wsContext; @Override public boolean login(String userId, String password) { MessageContext msgContext = wsContext.getMessageContext(); HttpSession httpSession = ((HttpServletRequest) msgContext.get(MessageContext.SERVLET_REQUEST)).getSession(); HttpServletRequest request = (HttpServletRequest)msgContext.get("HTTP.REQUEST"); System.out.println("request from " + request.getRemoteAddr()+", [userId = "+userId+", password="+password); httpSession.setAttribute("token", httpSession.getId()+"_"+userId); //save session into webservice context ((javax.servlet.ServletContext) msgContext.get(MessageContext.SERVLET_CONTEXT)).setAttribute("session", httpSession); return true; } @Override public boolean logout(String userId) { MessageContext msgContext = wsContext.getMessageContext(); HttpSession httpSession = (HttpSession)((ServletContext) msgContext.get(MessageContext.SERVLET_CONTEXT)).getAttribute("session"); String token = (String)httpSession.getAttribute("token"); if(null != userId && null != token && token.equals(httpSession.getId()+"_"+userId)){ httpSession.removeAttribute("token"); System.out.println("userId["+userId+"] log out success!"); return true; }else{ System.out.println("userId["+userId+"] log out fail!"); return false; } } @Override public boolean checkLoginState(String userId){ MessageContext msgContext = wsContext.getMessageContext(); HttpSession httpSession = (HttpSession)((ServletContext) msgContext.get(MessageContext.SERVLET_CONTEXT)).getAttribute("session"); String token = (String)httpSession.getAttribute("token"); return (null != userId && null != token && token.equals(httpSession.getId()+"_"+userId)); } }
@WebService public interface IFileTransfer { void uploadFile(String userId, FileInfo fileInfo); FileInfo downloadFile(String userId, FileInfo fileInfo); }
@Repository @WebService(endpointInterface = "net.log_cd.ws.IFileTransfer") public class FileTransferImpl implements IFileTransfer{ @Resource private WebServiceContext wsContext; @Resource private IUserLogin userLogin; @Override public void uploadFile(String userId, FileInfo fileInfo) { if(!userLogin.checkLoginState(userId)){ System.out.println("illegal operation!"); return; } OutputStream os = null; try{ if(fileInfo.getPosition() != 0){ os = FileUtils.openOutputStream(new File(fileInfo.getServerFilePath()), true); }else{ os = FileUtils.openOutputStream(new File(fileInfo.getServerFilePath()), false); } os.write(fileInfo.getBytes()); }catch(Exception e){ e.printStackTrace(); }finally{ IOUtils.closeQuietly(os); } } @Override public FileInfo downloadFile(String userId, FileInfo fileInfo) { if(!userLogin.checkLoginState(userId)){ System.out.println("illegal operation!"); return null; } InputStream innputStrean = null; try{ innputStrean = new FileInputStream(fileInfo.getServerFilePath()); innputStrean.skip(fileInfo.getPosition()); byte[] bytes = new byte[1024 * 1024]; int size = innputStrean.read(bytes); if (size > 0) { byte[] fixedBytes = Arrays.copyOfRange(bytes, 0, size); fileInfo.setBytes(fixedBytes); } else { fileInfo.setBytes(new byte[0]); } }catch(Exception e){ e.printStackTrace(); }finally{ IOUtils.closeQuietly(innputStrean); } return fileInfo; } }
二、相关POJO
public class FileInfo { private String clientFilePath; private String serverFilePath; private long position; private byte[] bytes; public String getClientFilePath() { return clientFilePath; } public void setClientFilePath(String clientFilePath) { this.clientFilePath = clientFilePath; } public String getServerFilePath() { return serverFilePath; } public void setServerFilePath(String serverFilePath) { this.serverFilePath = serverFilePath; } public long getPosition() { return position; } public void setPosition(long position) { this.position = position; } public byte[] getBytes() { return bytes; } public void setBytes(byte[] bytes) { this.bytes = bytes; } }
三、发布WebService服务
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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" /> <context:component-scan base-package="net.log_cd.ws"/> <jaxws:endpoint id="userLoginWebService" implementor="#userLoginImpl" address="/userLogin" /> <jaxws:endpoint id="fileTransferWebService" implementor="#fileTransferImpl" address="/fileTransfer" /> </beans>
四、客户端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:jaxws="http://cxf.apache.org/jaxws" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <context:component-scan base-package="com.log_cd.ws" /> <!-- proxy factory schema --> <bean id="userLogin" class="net.log_cd.ws.IUserLogin" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="net.log_cd.ws.IUserLogin" /> <property name="address" value="http://localhost:8080/cxf/ws/userLogin" /> </bean> <!-- proxy factory schema --> <bean id="fileTransfer" class="net.log_cd.ws.IFileTransfer" factory-bean="clientFactory2" factory-method="create" /> <bean id="clientFactory2" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="net.log_cd.ws.IFileTransfer" /> <property name="address" value="http://localhost:8080/cxf/ws/fileTransfer" /> </bean> </beans>
五、测试代码
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:cxf-ws-client.xml") public class CXFWebServiceSessionTest extends AbstractJUnit4SpringContextTests { public static final String clientFilePath = "D:/test/test.jpg"; public static final String serverFilePath = "D:/test/test_cp.jpg"; public static final String userId = "log_cd"; @Resource private IUserLogin userLogin; @Resource private IFileTransfer fileTransfer; @Test public void userLogin() { boolean bool = userLogin.login(userId, "85139999"); System.out.println(bool); Assert.isTrue(bool); } @Ignore public void uploadFile() { InputStream inputStream = null; try { FileInfo fileInfo = new FileInfo(); inputStream = new FileInputStream(clientFilePath); byte[] bytes = new byte[1024 * 1024]; while (true) { int size = inputStream.read(bytes); if (size <= 0) { break; } byte[] fixedBytes = Arrays.copyOfRange(bytes, 0, size); fileInfo.setClientFilePath(clientFilePath); fileInfo.setServerFilePath(serverFilePath); fileInfo.setBytes(fixedBytes); fileTransfer.uploadFile(userId, fileInfo); fileInfo.setPosition(fileInfo.getPosition() + fixedBytes.length); } } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } finally { IOUtils.closeQuietly(inputStream); } } @Test public void downloadFile() { FileInfo fileInfo = new FileInfo(); fileInfo.setServerFilePath(serverFilePath); long position = 0; while (true) { fileInfo.setPosition(position); fileInfo = fileTransfer.downloadFile(userId, fileInfo); if (fileInfo.getBytes().length <= 0) { break; } OutputStream outputStream = null; try { if (position != 0) { outputStream = FileUtils.openOutputStream(new File(clientFilePath), true); } else { outputStream = FileUtils.openOutputStream(new File(clientFilePath), false); } outputStream.write(fileInfo.getBytes()); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } finally { IOUtils.closeQuietly(outputStream); } position += fileInfo.getBytes().length; } } @Test public void userLogout() { boolean bool = userLogin.logout(userId); System.out.println(bool); Assert.isTrue(bool); } }
发表评论
-
SpringBoot开发WebService之Axis
2019-07-14 23:56 4663一、服务器端发布WebService服务 1、POM.xml文 ... -
SpringBoot开发WebService之CXF
2019-07-14 23:56 1220一、在服务器端的WebSerivce服务发布 1、POM.xm ... -
JDK6开发WebService及用户认证
2015-08-21 13:50 1390一、编写服务端并发布 1、定义接口 public inte ... -
WebService之CXF处理类型转换
2015-08-20 13:53 2708一、接口与实现 @WebService @SOAPBin ... -
以多种方式访问WebService
2015-08-20 13:58 6386一、Java访问WebService (1)使用Axis ... -
Exporting beans as web services using XFire
2015-08-20 14:07 1113Once XFire is in your buil ... -
Spring中的远程访问和web服务
2010-05-23 23:17 4490一、介绍 目前,Sp ... -
集成axis开发webservice
2009-09-16 09:09 3005一、基础知识 Web S ... -
spring使用RMI暴露服务
2008-07-11 14:03 3729(1)定义接口: package com.logcd.spri ... -
java RMI
2008-06-25 13:31 5139RMI限java之间,且使用Stream 技术(seri ...
相关推荐
WebService之CXF开发指南WebService之CXF开发指南WebService之CXF开发指南
借鉴网上例子,弄了一个SpringBoot开发WebService之CXF示例。
简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar...
现在主流的webService框架,一个是axis2,一个是cxf,本资源是基于cxf框架的简单例子
WebService之CXF开发指南
WebService_CXF_x509
WebService之CXF开发指南.rar
webservice之cxf实例............
WebService之CXF开发.pdfWebService之CXF开发.pdf
WebService的CXF整合Spring有源码和jar包
webservice apache-cxf-2.3.5
springboot整合webservice采用CXF技术,springboot整合webservice采用CXF技术
webservice 包括 cxf wss xml http等jar欢迎下载 亲测可用
这是一个webservice(Apache CXF) 简单案例
WebService CXF 详细教程 加上自带CXF文件
WebService CXF学习(入门篇)
WebService-CXF.ppt
webservice cxf spring jar包大全,下载,超好用的
一头扎进WebService之CXF Shiro Maven Lucene ActiveMq ExcelPOI JFreeChart视频教程
webservice-CXF-spring 实现server+client