2019独角兽企业重金招聘Python工程师标准>>>

本文链接:http://blog.csdn.net/kongxx/article/details/7540930

Apache CXF实战之一 Hello World Web Service

Apache CXF实战之二 集成Sping与Web容器

Apache CXF实战之三 传输Java对象

Apache CXF实战之四 构建RESTful Web Service

Apache CXF实战之五 压缩Web Service数据

Apache CXF实战之六 创建安全的Web Service

首先声明我知道有个协议叫ftp,也知道有种编程叫sock编程,但我就是碰到了server对外只开放80端口,并且还需要提供文件上传和下载功能的应用,那好吧,开始干活。

1. 首先是一个封装了服务器端文件路径,客户端文件路径和要传输的字节数组的MyFile类。

package com.googlecode.garbagecan.cxfstudy.filetransfer;public class MyFile {private String clientFile;private String serverFile;private long position;private byte[] bytes;public String getClientFile() {return clientFile;}public void setClientFile(String clientFile) {this.clientFile = clientFile;}public String getServerFile() {return serverFile;}public void setServerFile(String serverFile) {this.serverFile = serverFile;}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;}
}

2. 文件传输的Web Service接口

package com.googlecode.garbagecan.cxfstudy.filetransfer;import javax.jws.WebMethod;
import javax.jws.WebService;@WebService
public interface FileTransferService {@WebMethodvoid uploadFile(MyFile myFile) throws FileTransferException;@WebMethodMyFile downloadFile(MyFile myFile) throws FileTransferException;
}

3. 文件传输的Web Service接口实现类,主要是一些流的操作

package com.googlecode.garbagecan.cxfstudy.filetransfer;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;public class FileTransferServiceImpl implements FileTransferService {public void uploadFile(MyFile myFile) throws FileTransferException {OutputStream os = null;try {if (myFile.getPosition() != 0) {os = FileUtils.openOutputStream(new File(myFile.getServerFile()), true);} else {os = FileUtils.openOutputStream(new File(myFile.getServerFile()), false);}os.write(myFile.getBytes());} catch(IOException e) {throw new FileTransferException(e.getMessage(), e);} finally {IOUtils.closeQuietly(os);}}public MyFile downloadFile(MyFile myFile) throws FileTransferException {InputStream is = null;try {is = new FileInputStream(myFile.getServerFile());is.skip(myFile.getPosition());byte[] bytes = new byte[1024 * 1024];int size = is.read(bytes);if (size > 0) {byte[] fixedBytes = Arrays.copyOfRange(bytes, 0, size);myFile.setBytes(fixedBytes);} else {myFile.setBytes(new byte[0]);}} catch(IOException e) {throw new FileTransferException(e.getMessage(), e);} finally {IOUtils.closeQuietly(is);}return myFile;}
}

4. 一个简单的文件传输异常类

package com.googlecode.garbagecan.cxfstudy.filetransfer;public class FileTransferException extends Exception {private static final long serialVersionUID = 1L;public FileTransferException() {super();}public FileTransferException(String message, Throwable cause) {super(message, cause);}public FileTransferException(String message) {super(message);}public FileTransferException(Throwable cause) {super(cause);}
}

5. 下面是Server类用来发布web service

package com.googlecode.garbagecan.cxfstudy.filetransfer;import javax.xml.ws.Endpoint;public class FileTransferServer {public static void main(String[] args) throws Exception {Endpoint.publish("http://localhost:9000/ws/jaxws/fileTransferService", new FileTransferServiceImpl());}
}

6. 最后是Client类,用来发送文件上传和下载请求。

package com.googlecode.garbagecan.cxfstudy.filetransfer;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class FileTransferClient {private static final String address = "http://localhost:9000/ws/jaxws/fileTransferService";private static final String clientFile = "/home/fkong/temp/client/test.zip";private static final String serverFile = "/home/fkong/temp/server/test.zip";public static void main(String[] args) throws Exception {long start = System.currentTimeMillis();
//      uploadFile();
//      downloadFile();long stop = System.currentTimeMillis();System.out.println("Time: " + (stop - start));}private static void uploadFile() throws FileTransferException {InputStream is = null;try {MyFile myFile = new MyFile();is = new FileInputStream(clientFile);byte[] bytes = new byte[1024 * 1024];while (true) {int size = is.read(bytes);if (size <= 0) {break;}byte[] fixedBytes = Arrays.copyOfRange(bytes, 0, size);myFile.setClientFile(clientFile);myFile.setServerFile(serverFile);myFile.setBytes(fixedBytes);uploadFile(myFile);myFile.setPosition(myFile.getPosition() + fixedBytes.length);}} catch(IOException e) {throw new FileTransferException(e.getMessage(), e);} finally {IOUtils.closeQuietly(is);}}private static void uploadFile(MyFile myFile) throws FileTransferException {JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();factoryBean.setAddress(address);factoryBean.setServiceClass(FileTransferService.class);Object obj = factoryBean.create();FileTransferService service = (FileTransferService) obj;service.uploadFile(myFile);}private static void downloadFile() throws FileTransferException {MyFile myFile = new MyFile();myFile.setServerFile(serverFile);long position = 0;while (true) {myFile.setPosition(position);myFile = downloadFile(myFile);if (myFile.getBytes().length <= 0) {break;}OutputStream os = null;try {if (position != 0) {os = FileUtils.openOutputStream(new File(clientFile), true);} else {os = FileUtils.openOutputStream(new File(clientFile), false);}os.write(myFile.getBytes());} catch(IOException e) {throw new FileTransferException(e.getMessage(), e);} finally {IOUtils.closeQuietly(os);}position += myFile.getBytes().length;}}private static MyFile downloadFile(MyFile myFile) throws FileTransferException {JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();factoryBean.setAddress(address);factoryBean.setServiceClass(FileTransferService.class);Object obj = factoryBean.create();FileTransferService service = (FileTransferService) obj;return service.downloadFile(myFile);}
}

首先需要准备一个大一点的文件,然后修改代码中的clientFile和serverFile路径,然后分别打开uploadFile和downloadFile注释,运行程序,检查目标文件查看结果。

这个程序还是比较简单的,但基本生完成了文件上传下载功能,如果需要,也可以对这个程序再做点修改使其支持断点续传。

转载于:https://my.oschina.net/baochanghong/blog/423138

Apache CXF实战之七 使用Web Service传输文件相关推荐

  1. 使用apache CXF和maven开发Web Service

    来源:http://www.cnblogs.com/holbrook/archive/2012/12/12/2814821.html 目前主要的java webservice框架剩下了axis2和cx ...

  2. Apache CXF实战之六 创建安全的Web Service

    2019独角兽企业重金招聘Python工程师标准>>> 本文链接:http://blog.csdn.net/kongxx/article/details/7534035 Apache ...

  3. 用cxf公布和调用web service

    用cxf发布和调用web service 最近我们的系统需要和一个第三方系统对接,对接的方式是通过web service,所以就学习了一下这方面的东西 用CXF来做web service是比较简单的, ...

  4. 用cxf发布和调用web service

    用cxf发布和调用web service http://cxf.apache.org/docs/jax-ws-configuration.html  官方API helloword地址 用CXF来做w ...

  5. Apache CXF实战之二 集成Sping与Web容器

    2019独角兽企业重金招聘Python工程师标准>>> 书接上文,下面看看CXF怎样和spring集成. 1.创建HelloWorld 接口类 [java] view plainco ...

  6. 采用web service传输超大数据

    因为以前也没有做过相关的web service开发,对于Xfire也只是知道有这么一个框架.当然现在它已经变成apache基金会旗下的一个开源项目CXF.不过,现在依旧有很多公司还在用Xfire作we ...

  7. node.js 实现udp传输_Node.js实战15:通过udp传输文件。

    本文将要写一个udp服务器,和一个udp客户端,并实现客户端发送文件给服务器. 服务器端 代码如下:var dgram = require("dgram"); server(); ...

  8. WEB Service 下实现大数据量的传输

    Vs2005里面的,查询12000条记录,设置RemotingFormat = SerializationFormat.Binary; 再序列化,通过WebService传输,客户端接收,再反序列化, ...

  9. WEB Service 下实现大数据量的传输

    Vs2005里面的,查询12000条记录,设置RemotingFormat = SerializationFormat.Binary; 再序列化,通过WebService传输,客户端接收,再反序列化, ...

最新文章

  1. scikit learning curve学习曲线绘制
  2. LeetCode - Add Binary
  3. unity 脚本中 调用另一个脚本_Unity 2019.4 脚本生命周期
  4. linux程序崩溃时调用链,Linux 获取并分析程序崩溃时的调用堆栈
  5. 怎么解决64位Access与32位不能同时安装的问题
  6. Eclipse安装Rust插件 (Ubuntu)
  7. [Java] Scanner(new File( )) 从文件输入内容
  8. 为什么用python的时候特别卡_【后端开发】python为什么会运行慢
  9. JS学习总结(2)——变量
  10. Ubuntu 上安装 Node.js
  11. android 全局dns解析,Android中DNS解析
  12. 联想电脑重装win7系统详细图文教程
  13. C++900行代码实现中国象棋游戏规则以及相关功能
  14. 单源最短路径的迪克斯特拉(Dijkstra)算法
  15. SQL Server附加数据库错误5123,另一个进程正在调用
  16. 随性随笔_201508
  17. 【LeetCode】第290场单周赛 --- 记录一次AK周赛
  18. 学习JAVA需要掌握的英文单词
  19. GoogleVR怎样在普通场景和VR场景之间进行切换
  20. 西北大学软工专硕专业课面试可能会问到的问题

热门文章

  1. html网页商品销量滞后怎么做,iview 刷新滞后于html问题
  2. 释放链表内存C语言,最简单的链表删除第一个节点时释放内存的问题
  3. catia如何整列加工_非标零件如何用机器检测?能像人类一样查出问题吗?
  4. 大数据 挑战 机会_大数据可视化面临哪些挑战
  5. python a除以b_A除以B (Python)
  6. python os.path
  7. 20 Alarms, sigaction(), and Reentrant System Calls
  8. numeric.js
  9. python语言在命名上是什么敏感的_一文轻松掌握python语言命名规范规则
  10. 怎么创建数据表的实体类和业务类_SSM搭建二手市场交易平台(二):数据表设计...