设为首页 收藏本站
查看: 323|回复: 0

[经验分享] Apache CXF实战之七:使用Web Service传输文件

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-1-10 06:36:45 | 显示全部楼层 |阅读模式

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


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



  • packagecom.googlecode.garbagecan.cxfstudy.filetransfer;



  • publicclassMyFile{


  • privateStringclientFile;


  • privateStringserverFile;


  • privatelongposition;


  • privatebyte[]bytes;


  • publicStringgetClientFile(){

  • returnclientFile;
  • }


  • publicvoidsetClientFile(StringclientFile){

  • this.clientFile=clientFile;
  • }


  • publicStringgetServerFile(){

  • returnserverFile;
  • }


  • publicvoidsetServerFile(StringserverFile){

  • this.serverFile=serverFile;
  • }


  • publiclonggetPosition(){

  • returnposition;
  • }


  • publicvoidsetPosition(longposition){

  • this.position=position;
  • }


  • publicbyte[]getBytes(){

  • returnbytes;
  • }


  • publicvoidsetBytes(byte[]bytes){

  • this.bytes=bytes;
  • }
  • }


2. 文件传输的Web Service接口



  • packagecom.googlecode.garbagecan.cxfstudy.filetransfer;


  • importjavax.jws.WebMethod;

  • importjavax.jws.WebService;


  • @WebService

  • publicinterfaceFileTransferService{


  • @WebMethod

  • voiduploadFile(MyFilemyFile)throwsFileTransferException;


  • @WebMethod

  • MyFiledownloadFile(MyFilemyFile)throwsFileTransferException;
  • }


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



  • packagecom.googlecode.garbagecan.cxfstudy.filetransfer;


  • importjava.io.File;

  • importjava.io.FileInputStream;

  • importjava.io.IOException;

  • importjava.io.InputStream;

  • importjava.io.OutputStream;

  • importjava.util.Arrays;


  • importorg.apache.commons.io.FileUtils;

  • importorg.apache.commons.io.IOUtils;


  • publicclassFileTransferServiceImplimplementsFileTransferService{


  • publicvoiduploadFile(MyFilemyFile)throwsFileTransferException{

  • OutputStreamos=null;


  • try{

  • if(myFile.getPosition()!=0){

  • os=FileUtils.openOutputStream(newFile(myFile.getServerFile()),true);

  • }else{

  • os=FileUtils.openOutputStream(newFile(myFile.getServerFile()),false);
  • }
  • os.write(myFile.getBytes());

  • }catch(IOExceptione){

  • thrownewFileTransferException(e.getMessage(),e);

  • }finally{
  • IOUtils.closeQuietly(os);
  • }
  • }


  • publicMyFiledownloadFile(MyFilemyFile)throwsFileTransferException{

  • InputStreamis=null;


  • try{

  • is=newFileInputStream(myFile.getServerFile());
  • is.skip(myFile.getPosition());

  • byte[]bytes=newbyte[1024*1024];

  • intsize=is.read(bytes);

  • if(size>0){

  • byte[]fixedBytes=Arrays.copyOfRange(bytes,0,size);
  • myFile.setBytes(fixedBytes);

  • }else{

  • myFile.setBytes(newbyte[0]);
  • }

  • }catch(IOExceptione){

  • thrownewFileTransferException(e.getMessage(),e);

  • }finally{
  • IOUtils.closeQuietly(is);
  • }

  • returnmyFile;
  • }
  • }


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



  • packagecom.googlecode.garbagecan.cxfstudy.filetransfer;


  • publicclassFileTransferExceptionextendsException{


  • privatestaticfinallongserialVersionUID=1L;


  • publicFileTransferException(){

  • super();
  • }


  • publicFileTransferException(Stringmessage,Throwablecause){

  • super(message,cause);
  • }


  • publicFileTransferException(Stringmessage){

  • super(message);
  • }


  • publicFileTransferException(Throwablecause){

  • super(cause);
  • }
  • }


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



  • packagecom.googlecode.garbagecan.cxfstudy.filetransfer;


  • importjavax.xml.ws.Endpoint;


  • publicclassFileTransferServer{


  • publicstaticvoidmain(String[]args)throwsException{

  • Endpoint.publish("http://localhost:9000/ws/jaxws/fileTransferService",newFileTransferServiceImpl());
  • }
  • }


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



  • packagecom.googlecode.garbagecan.cxfstudy.filetransfer;

  • importjava.io.File;
  • importjava.io.FileInputStream;
  • importjava.io.IOException;
  • importjava.io.InputStream;
  • importjava.io.OutputStream;
  • importjava.util.Arrays;

  • importorg.apache.commons.io.FileUtils;
  • importorg.apache.commons.io.IOUtils;
  • importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;

  • publicclassFileTransferClient{


  • privatestaticfinalStringaddress="http://localhost:9000/ws/jaxws/fileTransferService";


  • privatestaticfinalStringclientFile="/home/fkong/temp/client/test.zip";

  • privatestaticfinalStringserverFile="/home/fkong/temp/server/test.zip";

  • publicstaticvoidmain(String[]args)throwsException{

  • longstart=System.currentTimeMillis();
  • //uploadFile();
  • //downloadFile();

  • longstop=System.currentTimeMillis();
  • System.out.println("Time:"+(stop-start));
  • }

  • privatestaticvoiduploadFile()throwsFileTransferException{

  • InputStreamis=null;
  • try{

  • MyFilemyFile=newMyFile();

  • is=newFileInputStream(clientFile);

  • byte[]bytes=newbyte[1024*1024];
  • while(true){

  • intsize=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(IOExceptione){
  • thrownewFileTransferException(e.getMessage(),e);
  • }finally{
  • IOUtils.closeQuietly(is);
  • }
  • }

  • privatestaticvoiduploadFile(MyFilemyFile)throwsFileTransferException{

  • JaxWsProxyFactoryBeanfactoryBean=newJaxWsProxyFactoryBean();
  • factoryBean.setAddress(address);
  • factoryBean.setServiceClass(FileTransferService.class);

  • Objectobj=factoryBean.create();


  • FileTransferServiceservice=(FileTransferService)obj;
  • service.uploadFile(myFile);
  • }

  • privatestaticvoiddownloadFile()throwsFileTransferException{

  • MyFilemyFile=newMyFile();
  • myFile.setServerFile(serverFile);

  • longposition=0;
  • while(true){
  • myFile.setPosition(position);

  • myFile=downloadFile(myFile);

  • if(myFile.getBytes().length<=0){
  • break;
  • }


  • OutputStreamos=null;
  • try{
  • if(position!=0){

  • os=FileUtils.openOutputStream(newFile(clientFile),true);
  • }else{

  • os=FileUtils.openOutputStream(newFile(clientFile),false);
  • }
  • os.write(myFile.getBytes());
  • }catch(IOExceptione){
  • thrownewFileTransferException(e.getMessage(),e);
  • }finally{
  • IOUtils.closeQuietly(os);
  • }

  • position+=myFile.getBytes().length;
  • }
  • }

  • privatestaticMyFiledownloadFile(MyFilemyFile)throwsFileTransferException{

  • JaxWsProxyFactoryBeanfactoryBean=newJaxWsProxyFactoryBean();
  • factoryBean.setAddress(address);
  • factoryBean.setServiceClass(FileTransferService.class);

  • Objectobj=factoryBean.create();


  • FileTransferServiceservice=(FileTransferService)obj;
  • returnservice.downloadFile(myFile);
  • }
  • }


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


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


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


【系列文章】




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

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

  • Apache CXF实战之四:构建RESTful Web Service

  • Apache CXF实战之三:传输Java对象

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

  • Apache CXF实战之一:Hello World Web Service

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-326146-1-1.html 上篇帖子: Apache CXF实战之七 使用Web Service传输文件 下篇帖子: 在Apache HTTPD服务器中使用DSO完全分析
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表